Merge pull request #10510 from geoffw0/staticfn

C++: Fix FPs for cpp/unused-static-function in files that were not extracted completely
This commit is contained in:
Geoffrey White
2022-10-18 14:53:49 +01:00
committed by GitHub
4 changed files with 56 additions and 8 deletions

View File

@@ -13,16 +13,32 @@
import cpp
pragma[noinline]
predicate possiblyIncompleteFile(File f) {
exists(Diagnostic d | d.getFile() = f and d.getSeverity() >= 3)
}
predicate immediatelyReachableFunction(Function f) {
not f.isStatic() or
exists(BlockExpr be | be.getFunction() = f) or
f instanceof MemberFunction or
f instanceof TemplateFunction or
f.getFile() instanceof HeaderFile or
f.getAnAttribute().hasName("constructor") or
f.getAnAttribute().hasName("destructor") or
f.getAnAttribute().hasName("used") or
not f.isStatic()
or
exists(BlockExpr be | be.getFunction() = f)
or
f instanceof MemberFunction
or
f instanceof TemplateFunction
or
f.getFile() instanceof HeaderFile
or
f.getAnAttribute().hasName("constructor")
or
f.getAnAttribute().hasName("destructor")
or
f.getAnAttribute().hasName("used")
or
f.getAnAttribute().hasName("unused")
or
// a compiler error in the same file suggests we may be missing data
possiblyIncompleteFile(f.getFile())
}
predicate immediatelyReachableVariable(Variable v) {

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Fixed false positives from the "Unused static function" (`cpp/unused-static-function`) query in files that had errors during compilation.

View File

@@ -0,0 +1,15 @@
// semmle-extractor-options: --expect_errors
static void my_function1_called() {} // GOOD
static void my_function2_called_after_error() {} // GOOD
static void my_function3_not_called() {} // BAD [NOT DETECTED]
int main(void) {
my_function1_called();
--- compilation stops here because this line is not valid C code ---
my_function2_called_after_error();
return 0;
}

View File

@@ -33,3 +33,16 @@ static void f6(void);
static void f5(void) { f6(); }
static void f6(void) { f5(); }
// f7 and f8 are reachable from `function_caller`
static int f7() { return 1; } // GOOD
static void f8() { } // GOOD
void function_caller()
{
auto my_lambda = []() {
return f7();
}();
f8();
}