t-8ch updated this revision to Diff 451072. t-8ch added a comment. Rework tests
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D131067/new/ https://reviews.llvm.org/D131067 Files: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp clang/test/Analysis/dead-stores.cpp Index: clang/test/Analysis/dead-stores.cpp =================================================================== --- clang/test/Analysis/dead-stores.cpp +++ clang/test/Analysis/dead-stores.cpp @@ -217,3 +217,35 @@ return i + j; } +//===----------------------------------------------------------------------===// +// Dead store checking involving reference parameters. +//===----------------------------------------------------------------------===// + +struct ReferenceParameter { + int *ptr; + + ReferenceParameter(int &i) : ptr(&i) {} + void function(int &i) { + ptr = &i; + } + + int value() { + return *ptr; + } +}; + +void referenceParameters() { + int i = 7; + ReferenceParameter r(i); + i = 8; + if (r.value() == 8) + ; + i = 9; // FIXME this is a false-negative + + int j = 10; + r.function(j); + j = 11; + if (r.value() == 11) + ; + j = 12; // FIXME this is a false-negative +} Index: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp @@ -483,12 +483,21 @@ void operator()(const Stmt *S) { // Check for '&'. Any VarDecl whose address has been taken we treat as // escaped. - // FIXME: What about references? if (auto *LE = dyn_cast<LambdaExpr>(S)) { findLambdaReferenceCaptures(LE); return; } + if (auto *CE = dyn_cast<CallExpr>(S)) { + findCallReferenceParameters(CE); + return; + } + + if (auto *CE = dyn_cast<CXXConstructExpr>(S)) { + findConstructorReferenceParameters(CE); + return; + } + const UnaryOperator *U = dyn_cast<UnaryOperator>(S); if (!U) return; @@ -522,6 +531,31 @@ Escaped.insert(VD); } } + + void findReferenceParameter(const FunctionDecl *FD, + CallExpr::const_arg_range Args) { + for (const auto It : llvm::zip(FD->parameters(), Args)) { + if (!std::get<0>(It)->getType()->isReferenceType()) + continue; + + auto *DRE = dyn_cast<DeclRefExpr>(std::get<1>(It)); + if (!DRE) + continue; + + if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) + Escaped.insert(VD); + } + } + + void findCallReferenceParameters(const CallExpr *CE) { + if (auto *FD = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl())) { + findReferenceParameter(FD, CE->arguments()); + } + } + + void findConstructorReferenceParameters(const CXXConstructExpr *CE) { + findReferenceParameter(CE->getConstructor(), CE->arguments()); + } }; } // end anonymous namespace
Index: clang/test/Analysis/dead-stores.cpp =================================================================== --- clang/test/Analysis/dead-stores.cpp +++ clang/test/Analysis/dead-stores.cpp @@ -217,3 +217,35 @@ return i + j; } +//===----------------------------------------------------------------------===// +// Dead store checking involving reference parameters. +//===----------------------------------------------------------------------===// + +struct ReferenceParameter { + int *ptr; + + ReferenceParameter(int &i) : ptr(&i) {} + void function(int &i) { + ptr = &i; + } + + int value() { + return *ptr; + } +}; + +void referenceParameters() { + int i = 7; + ReferenceParameter r(i); + i = 8; + if (r.value() == 8) + ; + i = 9; // FIXME this is a false-negative + + int j = 10; + r.function(j); + j = 11; + if (r.value() == 11) + ; + j = 12; // FIXME this is a false-negative +} Index: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp @@ -483,12 +483,21 @@ void operator()(const Stmt *S) { // Check for '&'. Any VarDecl whose address has been taken we treat as // escaped. - // FIXME: What about references? if (auto *LE = dyn_cast<LambdaExpr>(S)) { findLambdaReferenceCaptures(LE); return; } + if (auto *CE = dyn_cast<CallExpr>(S)) { + findCallReferenceParameters(CE); + return; + } + + if (auto *CE = dyn_cast<CXXConstructExpr>(S)) { + findConstructorReferenceParameters(CE); + return; + } + const UnaryOperator *U = dyn_cast<UnaryOperator>(S); if (!U) return; @@ -522,6 +531,31 @@ Escaped.insert(VD); } } + + void findReferenceParameter(const FunctionDecl *FD, + CallExpr::const_arg_range Args) { + for (const auto It : llvm::zip(FD->parameters(), Args)) { + if (!std::get<0>(It)->getType()->isReferenceType()) + continue; + + auto *DRE = dyn_cast<DeclRefExpr>(std::get<1>(It)); + if (!DRE) + continue; + + if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) + Escaped.insert(VD); + } + } + + void findCallReferenceParameters(const CallExpr *CE) { + if (auto *FD = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl())) { + findReferenceParameter(FD, CE->arguments()); + } + } + + void findConstructorReferenceParameters(const CXXConstructExpr *CE) { + findReferenceParameter(CE->getConstructor(), CE->arguments()); + } }; } // end anonymous namespace
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits