llvm-project/clang/test/Analysis/lambda-convert-to-func-ptr.cpp
flovent b8bda9d446
[clang][analyzer] Correctly handle lambda-converted function pointers (#144906)
For lambdas that are converted to C function pointers, 
```
int (*ret_zero)() = []() { return 0; };
```
clang will generate conversion method like:
```
CXXConversionDecl implicit used constexpr operator int (*)() 'int (*() const noexcept)()' inline
 -CompoundStmt
   -ReturnStmt
    -ImplicitCastExpr 'int (*)()' <FunctionToPointerDecay>
     -DeclRefExpr 'int ()' lvalue CXXMethod 0x5ddb6fe35b18 '__invoke' 'int ()'
-CXXMethodDecl implicit used __invoke 'int ()' static inline
 -CompoundStmt (empty)
```
Based on comment in Sema, `__invoke`'s function body is left empty
because it's will be filled in CodeGen, so in AST analysis phase we
should get lambda's `operator()` directly instead of calling `__invoke`
itself.
2025-06-25 16:15:01 +02:00

22 lines
612 B
C++

// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,debug.ExprInspection -analyzer-config inline-lambdas=true -verify %s
void clang_analyzer_eval(bool);
void basic() {
int (*ret_zero)() = []() { return 0; };
clang_analyzer_eval(ret_zero() == 0); // expected-warning{{TRUE}}
}
void withParam() {
int (*add_ten)(int) = [](int b) { return b + 10; };
clang_analyzer_eval(add_ten(1) == 11); // expected-warning{{TRUE}}
}
int callBack(int (*fp)(int), int x) {
return fp(x);
}
void passWithFunc() {
clang_analyzer_eval(callBack([](int x) { return x; }, 5) == 5); // expected-warning{{TRUE}}
}