diff --git a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp --- a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp +++ b/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(S)) { findLambdaReferenceCaptures(LE); return; } + if (auto *CE = dyn_cast(S)) { + findCallReferenceParameters(CE); + return; + } + + if (auto *CE = dyn_cast(S)) { + findConstructorReferenceParameters(CE); + return; + } + const UnaryOperator *U = dyn_cast(S); if (!U) return; @@ -522,6 +531,29 @@ Escaped.insert(VD); } } + + void findReferenceParameter(const FunctionDecl *FD, const Expr *const *Args) { + unsigned numParams = FD->getNumParams(); + for (unsigned i = 0; i < numParams; ++i) { + if (FD->getParamDecl(i)->getType()->isReferenceType()) { + if (auto *DRE = dyn_cast(Args[i])) { + if (auto *VD = dyn_cast(DRE->getDecl())) { + Escaped.insert(VD); + } + } + } + } + } + + void findCallReferenceParameters(const CallExpr *CE) { + if (auto *FD = dyn_cast(CE->getCalleeDecl())) { + findReferenceParameter(FD, CE->getArgs()); + } + } + + void findConstructorReferenceParameters(const CXXConstructExpr *CE) { + findReferenceParameter(CE->getConstructor(), CE->getArgs()); + } }; } // end anonymous namespace diff --git a/clang/test/Analysis/dead-stores.cpp b/clang/test/Analysis/dead-stores.cpp --- a/clang/test/Analysis/dead-stores.cpp +++ b/clang/test/Analysis/dead-stores.cpp @@ -217,3 +217,26 @@ return i + j; } +//===----------------------------------------------------------------------===// +// Dead store checking involving references. +//===----------------------------------------------------------------------===// + +void functionReferenceParameter(int &i) { + i = 5; // no warning +} + +struct constructorReferenceParameter { + constructorReferenceParameter(int &i) { + i = 5; // no warning + } +}; + +void referenceParameters() { + int i = 5; + functionReferenceParameter(i); + i = 6; + + int j = 7; + constructorReferenceParameter k(j); + j = 8; +}