Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1 | //=- AnalysisBasedWarnings.cpp - Sema warnings based on libAnalysis -*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines analysis_warnings::[Policy,Executor]. |
| 11 | // Together they are used by Sema to issue warnings based on inexpensive |
| 12 | // static analysis algorithms in libAnalysis. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/AnalysisBasedWarnings.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 19 | #include "clang/AST/EvaluatedExprVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
| 21 | #include "clang/AST/ExprObjC.h" |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 22 | #include "clang/AST/ParentMap.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/AST/StmtObjC.h" |
| 26 | #include "clang/AST/StmtVisitor.h" |
| 27 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/Analyses/Consumed.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Analysis/Analyses/ReachableCode.h" |
| 30 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
| 31 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/AnalysisContext.h" |
| 33 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 34 | #include "clang/Analysis/CFGStmtMap.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Basic/SourceLocation.h" |
| 36 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Lex/Preprocessor.h" |
| 38 | #include "clang/Sema/ScopeInfo.h" |
| 39 | #include "clang/Sema/SemaInternal.h" |
Alexander Kornienko | e61e562 | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/ArrayRef.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/BitVector.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/FoldingSet.h" |
| 43 | #include "llvm/ADT/ImmutableMap.h" |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/MapVector.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/PostOrderIterator.h" |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/SmallVector.h" |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/StringRef.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Casting.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 51 | #include <deque> |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 52 | #include <iterator> |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 53 | #include <vector> |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 54 | |
| 55 | using namespace clang; |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // Unreachable code analysis. |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | namespace { |
| 62 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 63 | Sema &S; |
| 64 | public: |
| 65 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 66 | |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 67 | void HandleUnreachable(reachable_code::UnreachableKind UK, |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 68 | SourceLocation L, |
| 69 | SourceRange SilenceableCondVal, |
| 70 | SourceRange R1, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 71 | SourceRange R2) override { |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 72 | unsigned diag = diag::warn_unreachable; |
| 73 | switch (UK) { |
| 74 | case reachable_code::UK_Break: |
| 75 | diag = diag::warn_unreachable_break; |
| 76 | break; |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 77 | case reachable_code::UK_Return: |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 78 | diag = diag::warn_unreachable_return; |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 79 | break; |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 80 | case reachable_code::UK_Loop_Increment: |
| 81 | diag = diag::warn_unreachable_loop_increment; |
| 82 | break; |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 83 | case reachable_code::UK_Other: |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | S.Diag(L, diag) << R1 << R2; |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 88 | |
| 89 | SourceLocation Open = SilenceableCondVal.getBegin(); |
| 90 | if (Open.isValid()) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 91 | SourceLocation Close = SilenceableCondVal.getEnd(); |
| 92 | Close = S.getLocForEndOfToken(Close); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 93 | if (Close.isValid()) { |
| 94 | S.Diag(Open, diag::note_unreachable_silence) |
| 95 | << FixItHint::CreateInsertion(Open, "/* DISABLES CODE */ (") |
| 96 | << FixItHint::CreateInsertion(Close, ")"); |
| 97 | } |
| 98 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 99 | } |
| 100 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 101 | } // anonymous namespace |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 102 | |
| 103 | /// CheckUnreachable - Check for unreachable code. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 104 | static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) { |
Ted Kremenek | c1b2875 | 2014-02-25 22:35:37 +0000 | [diff] [blame] | 105 | // As a heuristic prune all diagnostics not in the main file. Currently |
| 106 | // the majority of warnings in headers are false positives. These |
| 107 | // are largely caused by configuration state, e.g. preprocessor |
| 108 | // defined code, etc. |
| 109 | // |
| 110 | // Note that this is also a performance optimization. Analyzing |
| 111 | // headers many times can be expensive. |
| 112 | if (!S.getSourceManager().isInMainFile(AC.getDecl()->getLocStart())) |
| 113 | return; |
| 114 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 115 | UnreachableCodeHandler UC(S); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 116 | reachable_code::FindUnreachableCode(AC, S.getPreprocessor(), UC); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Benjamin Kramer | 3a00225 | 2015-02-16 16:53:12 +0000 | [diff] [blame] | 119 | namespace { |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 120 | /// \brief Warn on logical operator errors in CFGBuilder |
| 121 | class LogicalErrorHandler : public CFGCallback { |
| 122 | Sema &S; |
| 123 | |
| 124 | public: |
| 125 | LogicalErrorHandler(Sema &S) : CFGCallback(), S(S) {} |
| 126 | |
| 127 | static bool HasMacroID(const Expr *E) { |
| 128 | if (E->getExprLoc().isMacroID()) |
| 129 | return true; |
| 130 | |
| 131 | // Recurse to children. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 132 | for (const Stmt *SubStmt : E->children()) |
| 133 | if (const Expr *SubExpr = dyn_cast_or_null<Expr>(SubStmt)) |
| 134 | if (HasMacroID(SubExpr)) |
| 135 | return true; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 140 | void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) override { |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 141 | if (HasMacroID(B)) |
| 142 | return; |
| 143 | |
| 144 | SourceRange DiagRange = B->getSourceRange(); |
| 145 | S.Diag(B->getExprLoc(), diag::warn_tautological_overlap_comparison) |
| 146 | << DiagRange << isAlwaysTrue; |
| 147 | } |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 148 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 149 | void compareBitwiseEquality(const BinaryOperator *B, |
| 150 | bool isAlwaysTrue) override { |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 151 | if (HasMacroID(B)) |
| 152 | return; |
| 153 | |
| 154 | SourceRange DiagRange = B->getSourceRange(); |
| 155 | S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_always) |
| 156 | << DiagRange << isAlwaysTrue; |
| 157 | } |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 158 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 159 | } // anonymous namespace |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 161 | //===----------------------------------------------------------------------===// |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 162 | // Check for infinite self-recursion in functions |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 165 | // Returns true if the function is called anywhere within the CFGBlock. |
| 166 | // For member functions, the additional condition of being call from the |
| 167 | // this pointer is required. |
Duncan P. N. Exon Smith | f0eafc7 | 2015-07-23 20:11:47 +0000 | [diff] [blame] | 168 | static bool hasRecursiveCallInPath(const FunctionDecl *FD, CFGBlock &Block) { |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 169 | // Process all the Stmt's in this block to find any calls to FD. |
Duncan P. N. Exon Smith | f0eafc7 | 2015-07-23 20:11:47 +0000 | [diff] [blame] | 170 | for (const auto &B : Block) { |
| 171 | if (B.getKind() != CFGElement::Statement) |
| 172 | continue; |
| 173 | |
| 174 | const CallExpr *CE = dyn_cast<CallExpr>(B.getAs<CFGStmt>()->getStmt()); |
| 175 | if (!CE || !CE->getCalleeDecl() || |
| 176 | CE->getCalleeDecl()->getCanonicalDecl() != FD) |
| 177 | continue; |
| 178 | |
| 179 | // Skip function calls which are qualified with a templated class. |
| 180 | if (const DeclRefExpr *DRE = |
| 181 | dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreParenImpCasts())) { |
| 182 | if (NestedNameSpecifier *NNS = DRE->getQualifier()) { |
| 183 | if (NNS->getKind() == NestedNameSpecifier::TypeSpec && |
| 184 | isa<TemplateSpecializationType>(NNS->getAsType())) { |
| 185 | continue; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE); |
| 191 | if (!MCE || isa<CXXThisExpr>(MCE->getImplicitObjectArgument()) || |
| 192 | !MCE->getMethodDecl()->isVirtual()) |
| 193 | return true; |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 198 | // All blocks are in one of three states. States are ordered so that blocks |
| 199 | // can only move to higher states. |
| 200 | enum RecursiveState { |
| 201 | FoundNoPath, |
| 202 | FoundPath, |
| 203 | FoundPathWithNoRecursiveCall |
| 204 | }; |
| 205 | |
| 206 | // Returns true if there exists a path to the exit block and every path |
| 207 | // to the exit block passes through a call to FD. |
| 208 | static bool checkForRecursiveFunctionCall(const FunctionDecl *FD, CFG *cfg) { |
| 209 | |
| 210 | const unsigned ExitID = cfg->getExit().getBlockID(); |
| 211 | |
| 212 | // Mark all nodes as FoundNoPath, then set the status of the entry block. |
| 213 | SmallVector<RecursiveState, 16> States(cfg->getNumBlockIDs(), FoundNoPath); |
| 214 | States[cfg->getEntry().getBlockID()] = FoundPathWithNoRecursiveCall; |
| 215 | |
| 216 | // Make the processing stack and seed it with the entry block. |
| 217 | SmallVector<CFGBlock *, 16> Stack; |
| 218 | Stack.push_back(&cfg->getEntry()); |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 219 | |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 220 | while (!Stack.empty()) { |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 221 | CFGBlock *CurBlock = Stack.back(); |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 222 | Stack.pop_back(); |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 223 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 224 | unsigned ID = CurBlock->getBlockID(); |
| 225 | RecursiveState CurState = States[ID]; |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 226 | |
| 227 | if (CurState == FoundPathWithNoRecursiveCall) { |
| 228 | // Found a path to the exit node without a recursive call. |
| 229 | if (ExitID == ID) |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 230 | return false; |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 231 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 232 | // Only change state if the block has a recursive call. |
| 233 | if (hasRecursiveCallInPath(FD, *CurBlock)) |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 234 | CurState = FoundPath; |
| 235 | } |
| 236 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 237 | // Loop over successor blocks and add them to the Stack if their state |
| 238 | // changes. |
| 239 | for (auto I = CurBlock->succ_begin(), E = CurBlock->succ_end(); I != E; ++I) |
| 240 | if (*I) { |
| 241 | unsigned next_ID = (*I)->getBlockID(); |
| 242 | if (States[next_ID] < CurState) { |
| 243 | States[next_ID] = CurState; |
| 244 | Stack.push_back(*I); |
| 245 | } |
| 246 | } |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 247 | } |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 248 | |
| 249 | // Return true if the exit node is reachable, and only reachable through |
| 250 | // a recursive call. |
| 251 | return States[ExitID] == FoundPath; |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void checkRecursiveFunction(Sema &S, const FunctionDecl *FD, |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 255 | const Stmt *Body, AnalysisDeclContext &AC) { |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 256 | FD = FD->getCanonicalDecl(); |
| 257 | |
| 258 | // Only run on non-templated functions and non-templated members of |
| 259 | // templated classes. |
| 260 | if (FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate && |
| 261 | FD->getTemplatedKind() != FunctionDecl::TK_MemberSpecialization) |
| 262 | return; |
| 263 | |
| 264 | CFG *cfg = AC.getCFG(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 265 | if (!cfg) return; |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 266 | |
| 267 | // If the exit block is unreachable, skip processing the function. |
| 268 | if (cfg->getExit().pred_empty()) |
| 269 | return; |
| 270 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 271 | // Emit diagnostic if a recursive function call is detected for all paths. |
| 272 | if (checkForRecursiveFunctionCall(FD, cfg)) |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 273 | S.Diag(Body->getLocStart(), diag::warn_infinite_recursive_function); |
| 274 | } |
| 275 | |
| 276 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 277 | // Check for missing return value. |
| 278 | //===----------------------------------------------------------------------===// |
| 279 | |
John McCall | 5c6ec8c | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 280 | enum ControlFlowKind { |
| 281 | UnknownFallThrough, |
| 282 | NeverFallThrough, |
| 283 | MaybeFallThrough, |
| 284 | AlwaysFallThrough, |
| 285 | NeverFallThroughOrReturn |
| 286 | }; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 287 | |
| 288 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 289 | /// Statement that should return a value. |
| 290 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 291 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 292 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 293 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 294 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 295 | /// statement but we may return. We assume that functions not marked noreturn |
| 296 | /// will return. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 297 | static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 298 | CFG *cfg = AC.getCFG(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 299 | if (!cfg) return UnknownFallThrough; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 300 | |
| 301 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 302 | // confuse us, so we mark all live things first. |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 303 | llvm::BitVector live(cfg->getNumBlockIDs()); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 304 | unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(), |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 305 | live); |
| 306 | |
| 307 | bool AddEHEdges = AC.getAddEHEdges(); |
| 308 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 309 | // When there are things remaining dead, and we didn't add EH edges |
| 310 | // from CallExprs to the catch clauses, we have to go back and |
| 311 | // mark them as live. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 312 | for (const auto *B : *cfg) { |
| 313 | if (!live[B->getBlockID()]) { |
| 314 | if (B->pred_begin() == B->pred_end()) { |
| 315 | if (B->getTerminator() && isa<CXXTryStmt>(B->getTerminator())) |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 316 | // When not adding EH edges from calls, catch clauses |
| 317 | // can otherwise seem dead. Avoid noting them as dead. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 318 | count += reachable_code::ScanReachableFromBlock(B, live); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 319 | continue; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Now we know what is live, we check the live precessors of the exit block |
| 325 | // and look for fall through paths, being careful to ignore normal returns, |
| 326 | // and exceptional paths. |
| 327 | bool HasLiveReturn = false; |
| 328 | bool HasFakeEdge = false; |
| 329 | bool HasPlainEdge = false; |
| 330 | bool HasAbnormalEdge = false; |
Ted Kremenek | 5020574 | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 331 | |
| 332 | // Ignore default cases that aren't likely to be reachable because all |
| 333 | // enums in a switch(X) have explicit case statements. |
| 334 | CFGBlock::FilterOptions FO; |
| 335 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 336 | |
| 337 | for (CFGBlock::filtered_pred_iterator |
| 338 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { |
| 339 | const CFGBlock& B = **I; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 340 | if (!live[B.getBlockID()]) |
| 341 | continue; |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 342 | |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 343 | // Skip blocks which contain an element marked as no-return. They don't |
| 344 | // represent actually viable edges into the exit block, so mark them as |
| 345 | // abnormal. |
| 346 | if (B.hasNoReturnElement()) { |
| 347 | HasAbnormalEdge = true; |
| 348 | continue; |
| 349 | } |
| 350 | |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 351 | // Destructors can appear after the 'return' in the CFG. This is |
| 352 | // normal. We need to look pass the destructors for the return |
| 353 | // statement (if it exists). |
| 354 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 355 | |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 356 | for ( ; ri != re ; ++ri) |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 357 | if (ri->getAs<CFGStmt>()) |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 358 | break; |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 360 | // No more CFGElements in the block? |
| 361 | if (ri == re) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 362 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 363 | HasAbnormalEdge = true; |
| 364 | continue; |
| 365 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 366 | // A labeled empty statement, or the entry block... |
| 367 | HasPlainEdge = true; |
| 368 | continue; |
| 369 | } |
Ted Kremenek | ebe6260 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 370 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 371 | CFGStmt CS = ri->castAs<CFGStmt>(); |
Ted Kremenek | adfb445 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 372 | const Stmt *S = CS.getStmt(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 373 | if (isa<ReturnStmt>(S)) { |
| 374 | HasLiveReturn = true; |
| 375 | continue; |
| 376 | } |
| 377 | if (isa<ObjCAtThrowStmt>(S)) { |
| 378 | HasFakeEdge = true; |
| 379 | continue; |
| 380 | } |
| 381 | if (isa<CXXThrowExpr>(S)) { |
| 382 | HasFakeEdge = true; |
| 383 | continue; |
| 384 | } |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 385 | if (isa<MSAsmStmt>(S)) { |
| 386 | // TODO: Verify this is correct. |
| 387 | HasFakeEdge = true; |
| 388 | HasLiveReturn = true; |
| 389 | continue; |
| 390 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 391 | if (isa<CXXTryStmt>(S)) { |
| 392 | HasAbnormalEdge = true; |
| 393 | continue; |
| 394 | } |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 395 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 396 | == B.succ_end()) { |
| 397 | HasAbnormalEdge = true; |
| 398 | continue; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 399 | } |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 400 | |
| 401 | HasPlainEdge = true; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 402 | } |
| 403 | if (!HasPlainEdge) { |
| 404 | if (HasLiveReturn) |
| 405 | return NeverFallThrough; |
| 406 | return NeverFallThroughOrReturn; |
| 407 | } |
| 408 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 409 | return MaybeFallThrough; |
| 410 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 411 | // noreturn, that don't return. If people would like this warning to be more |
| 412 | // accurate, such functions should be marked as noreturn. |
| 413 | return AlwaysFallThrough; |
| 414 | } |
| 415 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 416 | namespace { |
| 417 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 418 | struct CheckFallThroughDiagnostics { |
| 419 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 420 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 421 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 422 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 423 | unsigned diag_NeverFallThroughOrReturn; |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 424 | enum { Function, Block, Lambda } funMode; |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 425 | SourceLocation FuncLoc; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 426 | |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 427 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 428 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 429 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 430 | D.diag_MaybeFallThrough_HasNoReturn = |
| 431 | diag::warn_falloff_noreturn_function; |
| 432 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 433 | diag::warn_maybe_falloff_nonvoid_function; |
| 434 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 435 | diag::warn_falloff_noreturn_function; |
| 436 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 437 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 438 | |
| 439 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 440 | // might be overridden by non-noreturn functions. |
| 441 | bool isVirtualMethod = false; |
| 442 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 443 | isVirtualMethod = Method->isVirtual(); |
| 444 | |
Douglas Gregor | 0de5720 | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 445 | // Don't suggest that template instantiations be marked "noreturn" |
| 446 | bool isTemplateInstantiation = false; |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 447 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func)) |
| 448 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
Douglas Gregor | 0de5720 | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 449 | |
| 450 | if (!isVirtualMethod && !isTemplateInstantiation) |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 451 | D.diag_NeverFallThroughOrReturn = |
| 452 | diag::warn_suggest_noreturn_function; |
| 453 | else |
| 454 | D.diag_NeverFallThroughOrReturn = 0; |
| 455 | |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 456 | D.funMode = Function; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 457 | return D; |
| 458 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 460 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 461 | CheckFallThroughDiagnostics D; |
| 462 | D.diag_MaybeFallThrough_HasNoReturn = |
| 463 | diag::err_noreturn_block_has_return_expr; |
| 464 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 465 | diag::err_maybe_falloff_nonvoid_block; |
| 466 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 467 | diag::err_noreturn_block_has_return_expr; |
| 468 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 469 | diag::err_falloff_nonvoid_block; |
Fariborz Jahanian | 5ce2279 | 2014-04-03 23:06:35 +0000 | [diff] [blame] | 470 | D.diag_NeverFallThroughOrReturn = 0; |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 471 | D.funMode = Block; |
| 472 | return D; |
| 473 | } |
| 474 | |
| 475 | static CheckFallThroughDiagnostics MakeForLambda() { |
| 476 | CheckFallThroughDiagnostics D; |
| 477 | D.diag_MaybeFallThrough_HasNoReturn = |
| 478 | diag::err_noreturn_lambda_has_return_expr; |
| 479 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 480 | diag::warn_maybe_falloff_nonvoid_lambda; |
| 481 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 482 | diag::err_noreturn_lambda_has_return_expr; |
| 483 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 484 | diag::warn_falloff_nonvoid_lambda; |
| 485 | D.diag_NeverFallThroughOrReturn = 0; |
| 486 | D.funMode = Lambda; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 487 | return D; |
| 488 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 489 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 490 | bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid, |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 491 | bool HasNoReturn) const { |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 492 | if (funMode == Function) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 493 | return (ReturnsVoid || |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 494 | D.isIgnored(diag::warn_maybe_falloff_nonvoid_function, |
| 495 | FuncLoc)) && |
| 496 | (!HasNoReturn || |
| 497 | D.isIgnored(diag::warn_noreturn_function_has_return_expr, |
| 498 | FuncLoc)) && |
| 499 | (!ReturnsVoid || |
| 500 | D.isIgnored(diag::warn_suggest_noreturn_block, FuncLoc)); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 501 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 503 | // For blocks / lambdas. |
Fariborz Jahanian | 5ce2279 | 2014-04-03 23:06:35 +0000 | [diff] [blame] | 504 | return ReturnsVoid && !HasNoReturn; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 505 | } |
| 506 | }; |
| 507 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 508 | } // anonymous namespace |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 509 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 510 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 511 | /// function that should return a value. Check that we don't fall off the end |
| 512 | /// of a noreturn function. We assume that functions and blocks not marked |
| 513 | /// noreturn will return. |
| 514 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 515 | const BlockExpr *blkExpr, |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 516 | const CheckFallThroughDiagnostics& CD, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 517 | AnalysisDeclContext &AC) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 518 | |
| 519 | bool ReturnsVoid = false; |
| 520 | bool HasNoReturn = false; |
| 521 | |
| 522 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 523 | ReturnsVoid = FD->getReturnType()->isVoidType(); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 524 | HasNoReturn = FD->isNoReturn(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 525 | } |
| 526 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 527 | ReturnsVoid = MD->getReturnType()->isVoidType(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 528 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 529 | } |
| 530 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 531 | QualType BlockTy = blkExpr->getType(); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 532 | if (const FunctionType *FT = |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 533 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 534 | if (FT->getReturnType()->isVoidType()) |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 535 | ReturnsVoid = true; |
| 536 | if (FT->getNoReturnAttr()) |
| 537 | HasNoReturn = true; |
| 538 | } |
| 539 | } |
| 540 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 541 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 542 | |
| 543 | // Short circuit for compilation speed. |
| 544 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 545 | return; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 546 | |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 547 | SourceLocation LBrace = Body->getLocStart(), RBrace = Body->getLocEnd(); |
| 548 | // Either in a function body compound statement, or a function-try-block. |
| 549 | switch (CheckFallThrough(AC)) { |
| 550 | case UnknownFallThrough: |
| 551 | break; |
John McCall | 5c6ec8c | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 552 | |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 553 | case MaybeFallThrough: |
| 554 | if (HasNoReturn) |
| 555 | S.Diag(RBrace, CD.diag_MaybeFallThrough_HasNoReturn); |
| 556 | else if (!ReturnsVoid) |
| 557 | S.Diag(RBrace, CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 558 | break; |
| 559 | case AlwaysFallThrough: |
| 560 | if (HasNoReturn) |
| 561 | S.Diag(RBrace, CD.diag_AlwaysFallThrough_HasNoReturn); |
| 562 | else if (!ReturnsVoid) |
| 563 | S.Diag(RBrace, CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 564 | break; |
| 565 | case NeverFallThroughOrReturn: |
| 566 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) { |
| 567 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 568 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 0 << FD; |
| 569 | } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 570 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 1 << MD; |
| 571 | } else { |
| 572 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn); |
Chandler Carruth | c841b6e | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 573 | } |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 574 | } |
| 575 | break; |
| 576 | case NeverFallThrough: |
| 577 | break; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 582 | // -Wuninitialized |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 585 | namespace { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 586 | /// ContainsReference - A visitor class to search for references to |
| 587 | /// a particular declaration (the needle) within any evaluated component of an |
| 588 | /// expression (recursively). |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 589 | class ContainsReference : public ConstEvaluatedExprVisitor<ContainsReference> { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 590 | bool FoundReference; |
| 591 | const DeclRefExpr *Needle; |
| 592 | |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 593 | public: |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 594 | typedef ConstEvaluatedExprVisitor<ContainsReference> Inherited; |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 595 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 596 | ContainsReference(ASTContext &Context, const DeclRefExpr *Needle) |
| 597 | : Inherited(Context), FoundReference(false), Needle(Needle) {} |
| 598 | |
| 599 | void VisitExpr(const Expr *E) { |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 600 | // Stop evaluating if we already have a reference. |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 601 | if (FoundReference) |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 602 | return; |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 603 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 604 | Inherited::VisitExpr(E); |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 605 | } |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 606 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 607 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 608 | if (E == Needle) |
| 609 | FoundReference = true; |
| 610 | else |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 611 | Inherited::VisitDeclRefExpr(E); |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 612 | } |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 613 | |
| 614 | bool doesContainReference() const { return FoundReference; } |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 615 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 616 | } // anonymous namespace |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 617 | |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 618 | static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) { |
Fariborz Jahanian | 429fadb | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 619 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 620 | if (VariableTy->isBlockPointerType() && |
| 621 | !VD->hasAttr<BlocksAttr>()) { |
Nico Weber | 3c68ee9 | 2014-07-08 23:46:20 +0000 | [diff] [blame] | 622 | S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization) |
| 623 | << VD->getDeclName() |
| 624 | << FixItHint::CreateInsertion(VD->getLocation(), "__block "); |
Fariborz Jahanian | 429fadb | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 625 | return true; |
| 626 | } |
Richard Smith | f7ec86a | 2013-09-20 00:27:40 +0000 | [diff] [blame] | 627 | |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 628 | // Don't issue a fixit if there is already an initializer. |
| 629 | if (VD->getInit()) |
| 630 | return false; |
Richard Trieu | 2cdcf82 | 2012-05-03 01:09:59 +0000 | [diff] [blame] | 631 | |
| 632 | // Don't suggest a fixit inside macros. |
| 633 | if (VD->getLocEnd().isMacroID()) |
| 634 | return false; |
| 635 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 636 | SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd()); |
Richard Smith | f7ec86a | 2013-09-20 00:27:40 +0000 | [diff] [blame] | 637 | |
| 638 | // Suggest possible initialization (if any). |
| 639 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 640 | if (Init.empty()) |
| 641 | return false; |
| 642 | |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 643 | S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName() |
| 644 | << FixItHint::CreateInsertion(Loc, Init); |
| 645 | return true; |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 648 | /// Create a fixit to remove an if-like statement, on the assumption that its |
| 649 | /// condition is CondVal. |
| 650 | static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then, |
| 651 | const Stmt *Else, bool CondVal, |
| 652 | FixItHint &Fixit1, FixItHint &Fixit2) { |
| 653 | if (CondVal) { |
| 654 | // If condition is always true, remove all but the 'then'. |
| 655 | Fixit1 = FixItHint::CreateRemoval( |
| 656 | CharSourceRange::getCharRange(If->getLocStart(), |
| 657 | Then->getLocStart())); |
| 658 | if (Else) { |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 659 | SourceLocation ElseKwLoc = S.getLocForEndOfToken(Then->getLocEnd()); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 660 | Fixit2 = FixItHint::CreateRemoval( |
| 661 | SourceRange(ElseKwLoc, Else->getLocEnd())); |
| 662 | } |
| 663 | } else { |
| 664 | // If condition is always false, remove all but the 'else'. |
| 665 | if (Else) |
| 666 | Fixit1 = FixItHint::CreateRemoval( |
| 667 | CharSourceRange::getCharRange(If->getLocStart(), |
| 668 | Else->getLocStart())); |
| 669 | else |
| 670 | Fixit1 = FixItHint::CreateRemoval(If->getSourceRange()); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /// DiagUninitUse -- Helper function to produce a diagnostic for an |
| 675 | /// uninitialized use of a variable. |
| 676 | static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use, |
| 677 | bool IsCapturedByBlock) { |
| 678 | bool Diagnosed = false; |
| 679 | |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 680 | switch (Use.getKind()) { |
| 681 | case UninitUse::Always: |
| 682 | S.Diag(Use.getUser()->getLocStart(), diag::warn_uninit_var) |
| 683 | << VD->getDeclName() << IsCapturedByBlock |
| 684 | << Use.getUser()->getSourceRange(); |
| 685 | return; |
| 686 | |
| 687 | case UninitUse::AfterDecl: |
| 688 | case UninitUse::AfterCall: |
| 689 | S.Diag(VD->getLocation(), diag::warn_sometimes_uninit_var) |
| 690 | << VD->getDeclName() << IsCapturedByBlock |
| 691 | << (Use.getKind() == UninitUse::AfterDecl ? 4 : 5) |
| 692 | << const_cast<DeclContext*>(VD->getLexicalDeclContext()) |
| 693 | << VD->getSourceRange(); |
| 694 | S.Diag(Use.getUser()->getLocStart(), diag::note_uninit_var_use) |
| 695 | << IsCapturedByBlock << Use.getUser()->getSourceRange(); |
| 696 | return; |
| 697 | |
| 698 | case UninitUse::Maybe: |
| 699 | case UninitUse::Sometimes: |
| 700 | // Carry on to report sometimes-uninitialized branches, if possible, |
| 701 | // or a 'may be used uninitialized' diagnostic otherwise. |
| 702 | break; |
| 703 | } |
| 704 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 705 | // Diagnose each branch which leads to a sometimes-uninitialized use. |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 706 | for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end(); |
| 707 | I != E; ++I) { |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 708 | assert(Use.getKind() == UninitUse::Sometimes); |
| 709 | |
| 710 | const Expr *User = Use.getUser(); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 711 | const Stmt *Term = I->Terminator; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 712 | |
| 713 | // Information used when building the diagnostic. |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 714 | unsigned DiagKind; |
David Blaikie | 1d202a6 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 715 | StringRef Str; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 716 | SourceRange Range; |
| 717 | |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 718 | // FixIts to suppress the diagnostic by removing the dead condition. |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 719 | // For all binary terminators, branch 0 is taken if the condition is true, |
| 720 | // and branch 1 is taken if the condition is false. |
| 721 | int RemoveDiagKind = -1; |
| 722 | const char *FixitStr = |
| 723 | S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false") |
| 724 | : (I->Output ? "1" : "0"); |
| 725 | FixItHint Fixit1, Fixit2; |
| 726 | |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 727 | switch (Term ? Term->getStmtClass() : Stmt::DeclStmtClass) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 728 | default: |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 729 | // Don't know how to report this. Just fall back to 'may be used |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 730 | // uninitialized'. FIXME: Can this happen? |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 731 | continue; |
| 732 | |
| 733 | // "condition is true / condition is false". |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 734 | case Stmt::IfStmtClass: { |
| 735 | const IfStmt *IS = cast<IfStmt>(Term); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 736 | DiagKind = 0; |
| 737 | Str = "if"; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 738 | Range = IS->getCond()->getSourceRange(); |
| 739 | RemoveDiagKind = 0; |
| 740 | CreateIfFixit(S, IS, IS->getThen(), IS->getElse(), |
| 741 | I->Output, Fixit1, Fixit2); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 742 | break; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 743 | } |
| 744 | case Stmt::ConditionalOperatorClass: { |
| 745 | const ConditionalOperator *CO = cast<ConditionalOperator>(Term); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 746 | DiagKind = 0; |
| 747 | Str = "?:"; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 748 | Range = CO->getCond()->getSourceRange(); |
| 749 | RemoveDiagKind = 0; |
| 750 | CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(), |
| 751 | I->Output, Fixit1, Fixit2); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 752 | break; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 753 | } |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 754 | case Stmt::BinaryOperatorClass: { |
| 755 | const BinaryOperator *BO = cast<BinaryOperator>(Term); |
| 756 | if (!BO->isLogicalOp()) |
| 757 | continue; |
| 758 | DiagKind = 0; |
| 759 | Str = BO->getOpcodeStr(); |
| 760 | Range = BO->getLHS()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 761 | RemoveDiagKind = 0; |
| 762 | if ((BO->getOpcode() == BO_LAnd && I->Output) || |
| 763 | (BO->getOpcode() == BO_LOr && !I->Output)) |
| 764 | // true && y -> y, false || y -> y. |
| 765 | Fixit1 = FixItHint::CreateRemoval(SourceRange(BO->getLocStart(), |
| 766 | BO->getOperatorLoc())); |
| 767 | else |
| 768 | // false && y -> false, true || y -> true. |
| 769 | Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 770 | break; |
| 771 | } |
| 772 | |
| 773 | // "loop is entered / loop is exited". |
| 774 | case Stmt::WhileStmtClass: |
| 775 | DiagKind = 1; |
| 776 | Str = "while"; |
| 777 | Range = cast<WhileStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 778 | RemoveDiagKind = 1; |
| 779 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 780 | break; |
| 781 | case Stmt::ForStmtClass: |
| 782 | DiagKind = 1; |
| 783 | Str = "for"; |
| 784 | Range = cast<ForStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 785 | RemoveDiagKind = 1; |
| 786 | if (I->Output) |
| 787 | Fixit1 = FixItHint::CreateRemoval(Range); |
| 788 | else |
| 789 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 790 | break; |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 791 | case Stmt::CXXForRangeStmtClass: |
| 792 | if (I->Output == 1) { |
| 793 | // The use occurs if a range-based for loop's body never executes. |
| 794 | // That may be impossible, and there's no syntactic fix for this, |
| 795 | // so treat it as a 'may be uninitialized' case. |
| 796 | continue; |
| 797 | } |
| 798 | DiagKind = 1; |
| 799 | Str = "for"; |
| 800 | Range = cast<CXXForRangeStmt>(Term)->getRangeInit()->getSourceRange(); |
| 801 | break; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 802 | |
| 803 | // "condition is true / loop is exited". |
| 804 | case Stmt::DoStmtClass: |
| 805 | DiagKind = 2; |
| 806 | Str = "do"; |
| 807 | Range = cast<DoStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 808 | RemoveDiagKind = 1; |
| 809 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | // "switch case is taken". |
| 813 | case Stmt::CaseStmtClass: |
| 814 | DiagKind = 3; |
| 815 | Str = "case"; |
| 816 | Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange(); |
| 817 | break; |
| 818 | case Stmt::DefaultStmtClass: |
| 819 | DiagKind = 3; |
| 820 | Str = "default"; |
| 821 | Range = cast<DefaultStmt>(Term)->getDefaultLoc(); |
| 822 | break; |
| 823 | } |
| 824 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 825 | S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var) |
| 826 | << VD->getDeclName() << IsCapturedByBlock << DiagKind |
| 827 | << Str << I->Output << Range; |
| 828 | S.Diag(User->getLocStart(), diag::note_uninit_var_use) |
| 829 | << IsCapturedByBlock << User->getSourceRange(); |
| 830 | if (RemoveDiagKind != -1) |
| 831 | S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond) |
| 832 | << RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2; |
| 833 | |
| 834 | Diagnosed = true; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 835 | } |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 836 | |
| 837 | if (!Diagnosed) |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 838 | S.Diag(Use.getUser()->getLocStart(), diag::warn_maybe_uninit_var) |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 839 | << VD->getDeclName() << IsCapturedByBlock |
| 840 | << Use.getUser()->getSourceRange(); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 843 | /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an |
| 844 | /// uninitialized variable. This manages the different forms of diagnostic |
| 845 | /// emitted for particular types of uses. Returns true if the use was diagnosed |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 846 | /// as a warning. If a particular use is one we omit warnings for, returns |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 847 | /// false. |
| 848 | static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD, |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 849 | const UninitUse &Use, |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 850 | bool alwaysReportSelfInit = false) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 851 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) { |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 852 | // Inspect the initializer of the variable declaration which is |
| 853 | // being referenced prior to its initialization. We emit |
| 854 | // specialized diagnostics for self-initialization, and we |
| 855 | // specifically avoid warning about self references which take the |
| 856 | // form of: |
| 857 | // |
| 858 | // int x = x; |
| 859 | // |
| 860 | // This is used to indicate to GCC that 'x' is intentionally left |
| 861 | // uninitialized. Proven code paths which access 'x' in |
| 862 | // an uninitialized state after this will still warn. |
| 863 | if (const Expr *Initializer = VD->getInit()) { |
| 864 | if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts()) |
| 865 | return false; |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 866 | |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 867 | ContainsReference CR(S.Context, DRE); |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 868 | CR.Visit(Initializer); |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 869 | if (CR.doesContainReference()) { |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 870 | S.Diag(DRE->getLocStart(), |
| 871 | diag::warn_uninit_self_reference_in_init) |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 872 | << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange(); |
| 873 | return true; |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 874 | } |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 875 | } |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 876 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 877 | DiagUninitUse(S, VD, Use, false); |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 878 | } else { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 879 | const BlockExpr *BE = cast<BlockExpr>(Use.getUser()); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 880 | if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>()) |
| 881 | S.Diag(BE->getLocStart(), |
| 882 | diag::warn_uninit_byref_blockvar_captured_by_block) |
Fariborz Jahanian | 429fadb | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 883 | << VD->getDeclName(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 884 | else |
| 885 | DiagUninitUse(S, VD, Use, true); |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // Report where the variable was declared when the use wasn't within |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 889 | // the initializer of that declaration & we didn't already suggest |
| 890 | // an initialization fixit. |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 891 | if (!SuggestInitializationFixit(S, VD)) |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 892 | S.Diag(VD->getLocStart(), diag::note_uninit_var_def) |
| 893 | << VD->getDeclName(); |
| 894 | |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 895 | return true; |
Chandler Carruth | 7a03720 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 898 | namespace { |
| 899 | class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> { |
| 900 | public: |
| 901 | FallthroughMapper(Sema &S) |
| 902 | : FoundSwitchStatements(false), |
| 903 | S(S) { |
| 904 | } |
| 905 | |
| 906 | bool foundSwitchStatements() const { return FoundSwitchStatements; } |
| 907 | |
| 908 | void markFallthroughVisited(const AttributedStmt *Stmt) { |
| 909 | bool Found = FallthroughStmts.erase(Stmt); |
| 910 | assert(Found); |
Kaelyn Uhrain | 29a8eeb | 2012-05-03 19:46:38 +0000 | [diff] [blame] | 911 | (void)Found; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts; |
| 915 | |
| 916 | const AttrStmts &getFallthroughStmts() const { |
| 917 | return FallthroughStmts; |
| 918 | } |
| 919 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 920 | void fillReachableBlocks(CFG *Cfg) { |
| 921 | assert(ReachableBlocks.empty() && "ReachableBlocks already filled"); |
| 922 | std::deque<const CFGBlock *> BlockQueue; |
| 923 | |
| 924 | ReachableBlocks.insert(&Cfg->getEntry()); |
| 925 | BlockQueue.push_back(&Cfg->getEntry()); |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 926 | // Mark all case blocks reachable to avoid problems with switching on |
| 927 | // constants, covered enums, etc. |
| 928 | // These blocks can contain fall-through annotations, and we don't want to |
| 929 | // issue a warn_fallthrough_attr_unreachable for them. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 930 | for (const auto *B : *Cfg) { |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 931 | const Stmt *L = B->getLabel(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 932 | if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B).second) |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 933 | BlockQueue.push_back(B); |
| 934 | } |
| 935 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 936 | while (!BlockQueue.empty()) { |
| 937 | const CFGBlock *P = BlockQueue.front(); |
| 938 | BlockQueue.pop_front(); |
| 939 | for (CFGBlock::const_succ_iterator I = P->succ_begin(), |
| 940 | E = P->succ_end(); |
| 941 | I != E; ++I) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 942 | if (*I && ReachableBlocks.insert(*I).second) |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 943 | BlockQueue.push_back(*I); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 948 | bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) { |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 949 | assert(!ReachableBlocks.empty() && "ReachableBlocks empty"); |
| 950 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 951 | int UnannotatedCnt = 0; |
| 952 | AnnotatedCnt = 0; |
| 953 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 954 | std::deque<const CFGBlock*> BlockQueue(B.pred_begin(), B.pred_end()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 955 | while (!BlockQueue.empty()) { |
| 956 | const CFGBlock *P = BlockQueue.front(); |
| 957 | BlockQueue.pop_front(); |
Nick Lewycky | cdf1108 | 2014-02-27 02:43:25 +0000 | [diff] [blame] | 958 | if (!P) continue; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 959 | |
| 960 | const Stmt *Term = P->getTerminator(); |
| 961 | if (Term && isa<SwitchStmt>(Term)) |
| 962 | continue; // Switch statement, good. |
| 963 | |
| 964 | const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel()); |
| 965 | if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end()) |
| 966 | continue; // Previous case label has no statements, good. |
| 967 | |
Alexander Kornienko | 09f15f3 | 2013-01-25 20:44:56 +0000 | [diff] [blame] | 968 | const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel()); |
| 969 | if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end()) |
| 970 | continue; // Case label is preceded with a normal label, good. |
| 971 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 972 | if (!ReachableBlocks.count(P)) { |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 973 | for (CFGBlock::const_reverse_iterator ElemIt = P->rbegin(), |
| 974 | ElemEnd = P->rend(); |
| 975 | ElemIt != ElemEnd; ++ElemIt) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 976 | if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) { |
| 977 | if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 978 | S.Diag(AS->getLocStart(), |
| 979 | diag::warn_fallthrough_attr_unreachable); |
| 980 | markFallthroughVisited(AS); |
| 981 | ++AnnotatedCnt; |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 982 | break; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 983 | } |
| 984 | // Don't care about other unreachable statements. |
| 985 | } |
| 986 | } |
| 987 | // If there are no unreachable statements, this may be a special |
| 988 | // case in CFG: |
| 989 | // case X: { |
| 990 | // A a; // A has a destructor. |
| 991 | // break; |
| 992 | // } |
| 993 | // // <<<< This place is represented by a 'hanging' CFG block. |
| 994 | // case Y: |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | const Stmt *LastStmt = getLastStmt(*P); |
| 999 | if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) { |
| 1000 | markFallthroughVisited(AS); |
| 1001 | ++AnnotatedCnt; |
| 1002 | continue; // Fallthrough annotation, good. |
| 1003 | } |
| 1004 | |
| 1005 | if (!LastStmt) { // This block contains no executable statements. |
| 1006 | // Traverse its predecessors. |
| 1007 | std::copy(P->pred_begin(), P->pred_end(), |
| 1008 | std::back_inserter(BlockQueue)); |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | ++UnannotatedCnt; |
| 1013 | } |
| 1014 | return !!UnannotatedCnt; |
| 1015 | } |
| 1016 | |
| 1017 | // RecursiveASTVisitor setup. |
| 1018 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 1019 | |
| 1020 | bool VisitAttributedStmt(AttributedStmt *S) { |
| 1021 | if (asFallThroughAttr(S)) |
| 1022 | FallthroughStmts.insert(S); |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
| 1026 | bool VisitSwitchStmt(SwitchStmt *S) { |
| 1027 | FoundSwitchStatements = true; |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
Alexander Kornienko | a9c809f | 2013-04-02 15:20:32 +0000 | [diff] [blame] | 1031 | // We don't want to traverse local type declarations. We analyze their |
| 1032 | // methods separately. |
| 1033 | bool TraverseDecl(Decl *D) { return true; } |
| 1034 | |
Alexander Kornienko | bf91164 | 2014-06-24 15:28:21 +0000 | [diff] [blame] | 1035 | // We analyze lambda bodies separately. Skip them here. |
| 1036 | bool TraverseLambdaBody(LambdaExpr *LE) { return true; } |
| 1037 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1038 | private: |
| 1039 | |
| 1040 | static const AttributedStmt *asFallThroughAttr(const Stmt *S) { |
| 1041 | if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) { |
| 1042 | if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs())) |
| 1043 | return AS; |
| 1044 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1045 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static const Stmt *getLastStmt(const CFGBlock &B) { |
| 1049 | if (const Stmt *Term = B.getTerminator()) |
| 1050 | return Term; |
| 1051 | for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(), |
| 1052 | ElemEnd = B.rend(); |
| 1053 | ElemIt != ElemEnd; ++ElemIt) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1054 | if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) |
| 1055 | return CS->getStmt(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1056 | } |
| 1057 | // Workaround to detect a statement thrown out by CFGBuilder: |
| 1058 | // case X: {} case Y: |
| 1059 | // case X: ; case Y: |
| 1060 | if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel())) |
| 1061 | if (!isa<SwitchCase>(SW->getSubStmt())) |
| 1062 | return SW->getSubStmt(); |
| 1063 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1064 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | bool FoundSwitchStatements; |
| 1068 | AttrStmts FallthroughStmts; |
| 1069 | Sema &S; |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1070 | llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1071 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1072 | } // anonymous namespace |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1073 | |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 1074 | static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC, |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 1075 | bool PerFunction) { |
Ted Kremenek | da5919f | 2012-11-12 21:20:48 +0000 | [diff] [blame] | 1076 | // Only perform this analysis when using C++11. There is no good workflow |
| 1077 | // for this warning when not using C++11. There is no good way to silence |
| 1078 | // the warning (no attribute is available) unless we are using C++11's support |
| 1079 | // for generalized attributes. Once could use pragmas to silence the warning, |
| 1080 | // but as a general solution that is gross and not in the spirit of this |
| 1081 | // warning. |
| 1082 | // |
| 1083 | // NOTE: This an intermediate solution. There are on-going discussions on |
| 1084 | // how to properly support this warning outside of C++11 with an annotation. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1085 | if (!AC.getASTContext().getLangOpts().CPlusPlus11) |
Ted Kremenek | da5919f | 2012-11-12 21:20:48 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1088 | FallthroughMapper FM(S); |
| 1089 | FM.TraverseStmt(AC.getBody()); |
| 1090 | |
| 1091 | if (!FM.foundSwitchStatements()) |
| 1092 | return; |
| 1093 | |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 1094 | if (PerFunction && FM.getFallthroughStmts().empty()) |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1097 | CFG *Cfg = AC.getCFG(); |
| 1098 | |
| 1099 | if (!Cfg) |
| 1100 | return; |
| 1101 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1102 | FM.fillReachableBlocks(Cfg); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1103 | |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 1104 | for (const CFGBlock *B : llvm::reverse(*Cfg)) { |
Alexander Kornienko | 5548879 | 2013-01-25 15:49:34 +0000 | [diff] [blame] | 1105 | const Stmt *Label = B->getLabel(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (!Label || !isa<SwitchCase>(Label)) |
| 1108 | continue; |
| 1109 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1110 | int AnnotatedCnt; |
| 1111 | |
Alexander Kornienko | 5548879 | 2013-01-25 15:49:34 +0000 | [diff] [blame] | 1112 | if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt)) |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1113 | continue; |
| 1114 | |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 1115 | S.Diag(Label->getLocStart(), |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 1116 | PerFunction ? diag::warn_unannotated_fallthrough_per_function |
| 1117 | : diag::warn_unannotated_fallthrough); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (!AnnotatedCnt) { |
| 1120 | SourceLocation L = Label->getLocStart(); |
| 1121 | if (L.isMacroID()) |
| 1122 | continue; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1123 | if (S.getLangOpts().CPlusPlus11) { |
Alexander Kornienko | 5548879 | 2013-01-25 15:49:34 +0000 | [diff] [blame] | 1124 | const Stmt *Term = B->getTerminator(); |
| 1125 | // Skip empty cases. |
| 1126 | while (B->empty() && !Term && B->succ_size() == 1) { |
| 1127 | B = *B->succ_begin(); |
| 1128 | Term = B->getTerminator(); |
| 1129 | } |
| 1130 | if (!(B->empty() && Term && isa<BreakStmt>(Term))) { |
Alexander Kornienko | e61e562 | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 1131 | Preprocessor &PP = S.getPreprocessor(); |
| 1132 | TokenValue Tokens[] = { |
| 1133 | tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"), |
| 1134 | tok::coloncolon, PP.getIdentifierInfo("fallthrough"), |
| 1135 | tok::r_square, tok::r_square |
| 1136 | }; |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 1137 | StringRef AnnotationSpelling = "[[clang::fallthrough]]"; |
| 1138 | StringRef MacroName = PP.getLastMacroWithSpelling(L, Tokens); |
| 1139 | if (!MacroName.empty()) |
| 1140 | AnnotationSpelling = MacroName; |
| 1141 | SmallString<64> TextToInsert(AnnotationSpelling); |
| 1142 | TextToInsert += "; "; |
Alexander Kornienko | 246e85d | 2012-05-26 00:49:15 +0000 | [diff] [blame] | 1143 | S.Diag(L, diag::note_insert_fallthrough_fixit) << |
Alexander Kornienko | e61e562 | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 1144 | AnnotationSpelling << |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 1145 | FixItHint::CreateInsertion(L, TextToInsert); |
Alexander Kornienko | 246e85d | 2012-05-26 00:49:15 +0000 | [diff] [blame] | 1146 | } |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1147 | } |
| 1148 | S.Diag(L, diag::note_insert_break_fixit) << |
| 1149 | FixItHint::CreateInsertion(L, "break; "); |
| 1150 | } |
| 1151 | } |
| 1152 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1153 | for (const auto *F : FM.getFallthroughStmts()) |
| 1154 | S.Diag(F->getLocStart(), diag::warn_fallthrough_attr_invalid_placement); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1157 | static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM, |
| 1158 | const Stmt *S) { |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1159 | assert(S); |
| 1160 | |
| 1161 | do { |
| 1162 | switch (S->getStmtClass()) { |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1163 | case Stmt::ForStmtClass: |
| 1164 | case Stmt::WhileStmtClass: |
| 1165 | case Stmt::CXXForRangeStmtClass: |
| 1166 | case Stmt::ObjCForCollectionStmtClass: |
| 1167 | return true; |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1168 | case Stmt::DoStmtClass: { |
| 1169 | const Expr *Cond = cast<DoStmt>(S)->getCond(); |
| 1170 | llvm::APSInt Val; |
| 1171 | if (!Cond->EvaluateAsInt(Val, Ctx)) |
| 1172 | return true; |
| 1173 | return Val.getBoolValue(); |
| 1174 | } |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1175 | default: |
| 1176 | break; |
| 1177 | } |
| 1178 | } while ((S = PM.getParent(S))); |
| 1179 | |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1183 | static void diagnoseRepeatedUseOfWeak(Sema &S, |
| 1184 | const sema::FunctionScopeInfo *CurFn, |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1185 | const Decl *D, |
| 1186 | const ParentMap &PM) { |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1187 | typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy; |
| 1188 | typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap; |
| 1189 | typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector; |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1190 | typedef std::pair<const Stmt *, WeakObjectUseMap::const_iterator> |
| 1191 | StmtUsesPair; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1192 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1193 | ASTContext &Ctx = S.getASTContext(); |
| 1194 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1195 | const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses(); |
| 1196 | |
| 1197 | // Extract all weak objects that are referenced more than once. |
| 1198 | SmallVector<StmtUsesPair, 8> UsesByStmt; |
| 1199 | for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end(); |
| 1200 | I != E; ++I) { |
| 1201 | const WeakUseVector &Uses = I->second; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Find the first read of the weak object. |
| 1204 | WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end(); |
| 1205 | for ( ; UI != UE; ++UI) { |
| 1206 | if (UI->isUnsafe()) |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | // If there were only writes to this object, don't warn. |
| 1211 | if (UI == UE) |
| 1212 | continue; |
| 1213 | |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1214 | // If there was only one read, followed by any number of writes, and the |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1215 | // read is not within a loop, don't warn. Additionally, don't warn in a |
| 1216 | // loop if the base object is a local variable -- local variables are often |
| 1217 | // changed in loops. |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1218 | if (UI == Uses.begin()) { |
| 1219 | WeakUseVector::const_iterator UI2 = UI; |
| 1220 | for (++UI2; UI2 != UE; ++UI2) |
| 1221 | if (UI2->isUnsafe()) |
| 1222 | break; |
| 1223 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1224 | if (UI2 == UE) { |
| 1225 | if (!isInLoop(Ctx, PM, UI->getUseExpr())) |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1226 | continue; |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1227 | |
| 1228 | const WeakObjectProfileTy &Profile = I->first; |
| 1229 | if (!Profile.isExactProfile()) |
| 1230 | continue; |
| 1231 | |
| 1232 | const NamedDecl *Base = Profile.getBase(); |
| 1233 | if (!Base) |
| 1234 | Base = Profile.getProperty(); |
| 1235 | assert(Base && "A profile always has a base or property."); |
| 1236 | |
| 1237 | if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base)) |
| 1238 | if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base)) |
| 1239 | continue; |
| 1240 | } |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1243 | UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I)); |
| 1244 | } |
| 1245 | |
| 1246 | if (UsesByStmt.empty()) |
| 1247 | return; |
| 1248 | |
| 1249 | // Sort by first use so that we emit the warnings in a deterministic order. |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1250 | SourceManager &SM = S.getSourceManager(); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1251 | std::sort(UsesByStmt.begin(), UsesByStmt.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1252 | [&SM](const StmtUsesPair &LHS, const StmtUsesPair &RHS) { |
| 1253 | return SM.isBeforeInTranslationUnit(LHS.first->getLocStart(), |
| 1254 | RHS.first->getLocStart()); |
| 1255 | }); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Classify the current code body for better warning text. |
| 1258 | // This enum should stay in sync with the cases in |
| 1259 | // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak. |
| 1260 | // FIXME: Should we use a common classification enum and the same set of |
| 1261 | // possibilities all throughout Sema? |
| 1262 | enum { |
| 1263 | Function, |
| 1264 | Method, |
| 1265 | Block, |
| 1266 | Lambda |
| 1267 | } FunctionKind; |
| 1268 | |
| 1269 | if (isa<sema::BlockScopeInfo>(CurFn)) |
| 1270 | FunctionKind = Block; |
| 1271 | else if (isa<sema::LambdaScopeInfo>(CurFn)) |
| 1272 | FunctionKind = Lambda; |
| 1273 | else if (isa<ObjCMethodDecl>(D)) |
| 1274 | FunctionKind = Method; |
| 1275 | else |
| 1276 | FunctionKind = Function; |
| 1277 | |
| 1278 | // Iterate through the sorted problems and emit warnings for each. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1279 | for (const auto &P : UsesByStmt) { |
| 1280 | const Stmt *FirstRead = P.first; |
| 1281 | const WeakObjectProfileTy &Key = P.second->first; |
| 1282 | const WeakUseVector &Uses = P.second->second; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1283 | |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1284 | // For complicated expressions like 'a.b.c' and 'x.b.c', WeakObjectProfileTy |
| 1285 | // may not contain enough information to determine that these are different |
| 1286 | // properties. We can only be 100% sure of a repeated use in certain cases, |
| 1287 | // and we adjust the diagnostic kind accordingly so that the less certain |
| 1288 | // case can be turned off if it is too noisy. |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1289 | unsigned DiagKind; |
| 1290 | if (Key.isExactProfile()) |
| 1291 | DiagKind = diag::warn_arc_repeated_use_of_weak; |
| 1292 | else |
| 1293 | DiagKind = diag::warn_arc_possible_repeated_use_of_weak; |
| 1294 | |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1295 | // Classify the weak object being accessed for better warning text. |
| 1296 | // This enum should stay in sync with the cases in |
| 1297 | // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak. |
| 1298 | enum { |
| 1299 | Variable, |
| 1300 | Property, |
| 1301 | ImplicitProperty, |
| 1302 | Ivar |
| 1303 | } ObjectKind; |
| 1304 | |
| 1305 | const NamedDecl *D = Key.getProperty(); |
| 1306 | if (isa<VarDecl>(D)) |
| 1307 | ObjectKind = Variable; |
| 1308 | else if (isa<ObjCPropertyDecl>(D)) |
| 1309 | ObjectKind = Property; |
| 1310 | else if (isa<ObjCMethodDecl>(D)) |
| 1311 | ObjectKind = ImplicitProperty; |
| 1312 | else if (isa<ObjCIvarDecl>(D)) |
| 1313 | ObjectKind = Ivar; |
| 1314 | else |
| 1315 | llvm_unreachable("Unexpected weak object kind!"); |
| 1316 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1317 | // Show the first time the object was read. |
| 1318 | S.Diag(FirstRead->getLocStart(), DiagKind) |
Joerg Sonnenberger | ffc6d49 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 1319 | << int(ObjectKind) << D << int(FunctionKind) |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1320 | << FirstRead->getSourceRange(); |
| 1321 | |
| 1322 | // Print all the other accesses as notes. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1323 | for (const auto &Use : Uses) { |
| 1324 | if (Use.getUseExpr() == FirstRead) |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1325 | continue; |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1326 | S.Diag(Use.getUseExpr()->getLocStart(), |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1327 | diag::note_arc_weak_also_accessed_here) |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1328 | << Use.getUseExpr()->getSourceRange(); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1333 | namespace { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1334 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 1335 | Sema &S; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1336 | typedef SmallVector<UninitUse, 2> UsesVec; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1337 | typedef llvm::PointerIntPair<UsesVec *, 1, bool> MappedType; |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 1338 | // Prefer using MapVector to DenseMap, so that iteration order will be |
| 1339 | // the same as insertion order. This is needed to obtain a deterministic |
| 1340 | // order of diagnostics when calling flushDiagnostics(). |
| 1341 | typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap; |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame^] | 1342 | UsesMap uses; |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1343 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1344 | public: |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame^] | 1345 | UninitValsDiagReporter(Sema &S) : S(S) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1346 | ~UninitValsDiagReporter() override { flushDiagnostics(); } |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1347 | |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 1348 | MappedType &getUses(const VarDecl *vd) { |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame^] | 1349 | MappedType &V = uses[vd]; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1350 | if (!V.getPointer()) |
| 1351 | V.setPointer(new UsesVec()); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1352 | return V; |
| 1353 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1354 | |
| 1355 | void handleUseOfUninitVariable(const VarDecl *vd, |
| 1356 | const UninitUse &use) override { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1357 | getUses(vd).getPointer()->push_back(use); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1360 | void handleSelfInit(const VarDecl *vd) override { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1361 | getUses(vd).setInt(true); |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | void flushDiagnostics() { |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame^] | 1365 | for (const auto &P : uses) { |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1366 | const VarDecl *vd = P.first; |
| 1367 | const MappedType &V = P.second; |
Ted Kremenek | b3dbe28 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 1368 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1369 | UsesVec *vec = V.getPointer(); |
| 1370 | bool hasSelfInit = V.getInt(); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Specially handle the case where we have uses of an uninitialized |
| 1373 | // variable, but the root cause is an idiomatic self-init. We want |
| 1374 | // to report the diagnostic at the self-init since that is the root cause. |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1375 | if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec)) |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1376 | DiagnoseUninitializedUse(S, vd, |
| 1377 | UninitUse(vd->getInit()->IgnoreParenCasts(), |
| 1378 | /* isAlwaysUninit */ true), |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1379 | /* alwaysReportSelfInit */ true); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1380 | else { |
| 1381 | // Sort the uses by their SourceLocations. While not strictly |
| 1382 | // guaranteed to produce them in line/column order, this will provide |
| 1383 | // a stable ordering. |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1384 | std::sort(vec->begin(), vec->end(), |
| 1385 | [](const UninitUse &a, const UninitUse &b) { |
| 1386 | // Prefer a more confident report over a less confident one. |
| 1387 | if (a.getKind() != b.getKind()) |
| 1388 | return a.getKind() > b.getKind(); |
| 1389 | return a.getUser()->getLocStart() < b.getUser()->getLocStart(); |
| 1390 | }); |
| 1391 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1392 | for (const auto &U : *vec) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1393 | // If we have self-init, downgrade all uses to 'may be uninitialized'. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1394 | UninitUse Use = hasSelfInit ? UninitUse(U.getUser(), false) : U; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (DiagnoseUninitializedUse(S, vd, Use)) |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1397 | // Skip further diagnostics for this variable. We try to warn only |
| 1398 | // on the first point at which a variable is used uninitialized. |
| 1399 | break; |
| 1400 | } |
Chandler Carruth | 7a03720 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 1401 | } |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1402 | |
| 1403 | // Release the uses vector. |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1404 | delete vec; |
| 1405 | } |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame^] | 1406 | |
| 1407 | uses.clear(); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1408 | } |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1409 | |
| 1410 | private: |
| 1411 | static bool hasAlwaysUninitializedUse(const UsesVec* vec) { |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1412 | return std::any_of(vec->begin(), vec->end(), [](const UninitUse &U) { |
| 1413 | return U.getKind() == UninitUse::Always || |
| 1414 | U.getKind() == UninitUse::AfterCall || |
| 1415 | U.getKind() == UninitUse::AfterDecl; |
| 1416 | }); |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1417 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1418 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1419 | } // anonymous namespace |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1420 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1421 | namespace clang { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1422 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1423 | typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1424 | typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag; |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1425 | typedef std::list<DelayedDiag> DiagList; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1426 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1427 | struct SortDiagBySourceLocation { |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1428 | SourceManager &SM; |
| 1429 | SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {} |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1430 | |
| 1431 | bool operator()(const DelayedDiag &left, const DelayedDiag &right) { |
| 1432 | // Although this call will be slow, this is only called when outputting |
| 1433 | // multiple warnings. |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1434 | return SM.isBeforeInTranslationUnit(left.first.first, right.first.first); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1435 | } |
| 1436 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1437 | } // anonymous namespace |
| 1438 | } // namespace clang |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1439 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1440 | //===----------------------------------------------------------------------===// |
| 1441 | // -Wthread-safety |
| 1442 | //===----------------------------------------------------------------------===// |
| 1443 | namespace clang { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1444 | namespace threadSafety { |
Benjamin Kramer | 539803c | 2015-03-19 14:23:45 +0000 | [diff] [blame] | 1445 | namespace { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1446 | class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1447 | Sema &S; |
| 1448 | DiagList Warnings; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1449 | SourceLocation FunLocation, FunEndLocation; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1450 | |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1451 | const FunctionDecl *CurrentFunction; |
| 1452 | bool Verbose; |
| 1453 | |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1454 | OptionalNotes getNotes() const { |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1455 | if (Verbose && CurrentFunction) { |
| 1456 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(), |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1457 | S.PDiag(diag::note_thread_warning_in_fun) |
| 1458 | << CurrentFunction->getNameAsString()); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1459 | return OptionalNotes(1, FNote); |
| 1460 | } |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1461 | return OptionalNotes(); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1464 | OptionalNotes getNotes(const PartialDiagnosticAt &Note) const { |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1465 | OptionalNotes ONS(1, Note); |
| 1466 | if (Verbose && CurrentFunction) { |
| 1467 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(), |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1468 | S.PDiag(diag::note_thread_warning_in_fun) |
| 1469 | << CurrentFunction->getNameAsString()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1470 | ONS.push_back(std::move(FNote)); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1471 | } |
| 1472 | return ONS; |
| 1473 | } |
| 1474 | |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1475 | OptionalNotes getNotes(const PartialDiagnosticAt &Note1, |
| 1476 | const PartialDiagnosticAt &Note2) const { |
| 1477 | OptionalNotes ONS; |
| 1478 | ONS.push_back(Note1); |
| 1479 | ONS.push_back(Note2); |
| 1480 | if (Verbose && CurrentFunction) { |
| 1481 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(), |
| 1482 | S.PDiag(diag::note_thread_warning_in_fun) |
| 1483 | << CurrentFunction->getNameAsString()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1484 | ONS.push_back(std::move(FNote)); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1485 | } |
| 1486 | return ONS; |
| 1487 | } |
| 1488 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1489 | // Helper functions |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1490 | void warnLockMismatch(unsigned DiagID, StringRef Kind, Name LockName, |
| 1491 | SourceLocation Loc) { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1492 | // Gracefully handle rare cases when the analysis can't get a more |
| 1493 | // precise source location. |
| 1494 | if (!Loc.isValid()) |
| 1495 | Loc = FunLocation; |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1496 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind << LockName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1497 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | public: |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1501 | ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL) |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1502 | : S(S), FunLocation(FL), FunEndLocation(FEL), |
| 1503 | CurrentFunction(nullptr), Verbose(false) {} |
| 1504 | |
| 1505 | void setVerbose(bool b) { Verbose = b; } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1506 | |
| 1507 | /// \brief Emit all buffered diagnostics in order of sourcelocation. |
| 1508 | /// We need to output diagnostics produced while iterating through |
| 1509 | /// the lockset in deterministic order, so this function orders diagnostics |
| 1510 | /// and outputs them. |
| 1511 | void emitDiagnostics() { |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1512 | Warnings.sort(SortDiagBySourceLocation(S.getSourceManager())); |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1513 | for (const auto &Diag : Warnings) { |
| 1514 | S.Diag(Diag.first.first, Diag.first.second); |
| 1515 | for (const auto &Note : Diag.second) |
| 1516 | S.Diag(Note.first, Note.second); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1517 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1520 | void handleInvalidLockExp(StringRef Kind, SourceLocation Loc) override { |
| 1521 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_cannot_resolve_lock) |
| 1522 | << Loc); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1523 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | ff2f3f8 | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 1524 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1525 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1526 | void handleUnmatchedUnlock(StringRef Kind, Name LockName, |
| 1527 | SourceLocation Loc) override { |
| 1528 | warnLockMismatch(diag::warn_unlock_but_no_lock, Kind, LockName, Loc); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1529 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1530 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1531 | void handleIncorrectUnlockKind(StringRef Kind, Name LockName, |
| 1532 | LockKind Expected, LockKind Received, |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1533 | SourceLocation Loc) override { |
| 1534 | if (Loc.isInvalid()) |
| 1535 | Loc = FunLocation; |
| 1536 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unlock_kind_mismatch) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1537 | << Kind << LockName << Received |
| 1538 | << Expected); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1539 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1540 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1541 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1542 | void handleDoubleLock(StringRef Kind, Name LockName, SourceLocation Loc) override { |
| 1543 | warnLockMismatch(diag::warn_double_lock, Kind, LockName, Loc); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1546 | void handleMutexHeldEndOfScope(StringRef Kind, Name LockName, |
| 1547 | SourceLocation LocLocked, |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1548 | SourceLocation LocEndOfScope, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1549 | LockErrorKind LEK) override { |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1550 | unsigned DiagID = 0; |
| 1551 | switch (LEK) { |
| 1552 | case LEK_LockedSomePredecessors: |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1553 | DiagID = diag::warn_lock_some_predecessors; |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1554 | break; |
| 1555 | case LEK_LockedSomeLoopIterations: |
| 1556 | DiagID = diag::warn_expecting_lock_held_on_loop; |
| 1557 | break; |
| 1558 | case LEK_LockedAtEndOfFunction: |
| 1559 | DiagID = diag::warn_no_unlock; |
| 1560 | break; |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 1561 | case LEK_NotLockedAtEndOfFunction: |
| 1562 | DiagID = diag::warn_expecting_locked; |
| 1563 | break; |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1564 | } |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1565 | if (LocEndOfScope.isInvalid()) |
| 1566 | LocEndOfScope = FunEndLocation; |
| 1567 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1568 | PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << Kind |
| 1569 | << LockName); |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 1570 | if (LocLocked.isValid()) { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1571 | PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here) |
| 1572 | << Kind); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1573 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 1574 | return; |
| 1575 | } |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1576 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1579 | void handleExclusiveAndShared(StringRef Kind, Name LockName, |
| 1580 | SourceLocation Loc1, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1581 | SourceLocation Loc2) override { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1582 | PartialDiagnosticAt Warning(Loc1, |
| 1583 | S.PDiag(diag::warn_lock_exclusive_and_shared) |
| 1584 | << Kind << LockName); |
| 1585 | PartialDiagnosticAt Note(Loc2, S.PDiag(diag::note_lock_exclusive_and_shared) |
| 1586 | << Kind << LockName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1587 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1590 | void handleNoMutexHeld(StringRef Kind, const NamedDecl *D, |
| 1591 | ProtectedOperationKind POK, AccessKind AK, |
| 1592 | SourceLocation Loc) override { |
| 1593 | assert((POK == POK_VarAccess || POK == POK_VarDereference) && |
| 1594 | "Only works for variables"); |
Caitlin Sadowski | e50d8c3 | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 1595 | unsigned DiagID = POK == POK_VarAccess? |
| 1596 | diag::warn_variable_requires_any_lock: |
| 1597 | diag::warn_var_deref_requires_any_lock; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1598 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) |
DeLesley Hutchins | a15e1b4 | 2012-09-19 19:18:29 +0000 | [diff] [blame] | 1599 | << D->getNameAsString() << getLockKindFromAccessKind(AK)); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1600 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1603 | void handleMutexNotHeld(StringRef Kind, const NamedDecl *D, |
| 1604 | ProtectedOperationKind POK, Name LockName, |
| 1605 | LockKind LK, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1606 | Name *PossibleMatch) override { |
Caitlin Sadowski | 427f42e | 2011-09-13 18:01:58 +0000 | [diff] [blame] | 1607 | unsigned DiagID = 0; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1608 | if (PossibleMatch) { |
| 1609 | switch (POK) { |
| 1610 | case POK_VarAccess: |
| 1611 | DiagID = diag::warn_variable_requires_lock_precise; |
| 1612 | break; |
| 1613 | case POK_VarDereference: |
| 1614 | DiagID = diag::warn_var_deref_requires_lock_precise; |
| 1615 | break; |
| 1616 | case POK_FunctionCall: |
| 1617 | DiagID = diag::warn_fun_requires_lock_precise; |
| 1618 | break; |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1619 | case POK_PassByRef: |
| 1620 | DiagID = diag::warn_guarded_pass_by_reference; |
| 1621 | break; |
| 1622 | case POK_PtPassByRef: |
| 1623 | DiagID = diag::warn_pt_guarded_pass_by_reference; |
| 1624 | break; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1625 | } |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1626 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind |
| 1627 | << D->getNameAsString() |
| 1628 | << LockName << LK); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1629 | PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1630 | << *PossibleMatch); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1631 | if (Verbose && POK == POK_VarAccess) { |
| 1632 | PartialDiagnosticAt VNote(D->getLocation(), |
| 1633 | S.PDiag(diag::note_guarded_by_declared_here) |
| 1634 | << D->getNameAsString()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1635 | Warnings.emplace_back(std::move(Warning), getNotes(Note, VNote)); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1636 | } else |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1637 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1638 | } else { |
| 1639 | switch (POK) { |
| 1640 | case POK_VarAccess: |
| 1641 | DiagID = diag::warn_variable_requires_lock; |
| 1642 | break; |
| 1643 | case POK_VarDereference: |
| 1644 | DiagID = diag::warn_var_deref_requires_lock; |
| 1645 | break; |
| 1646 | case POK_FunctionCall: |
| 1647 | DiagID = diag::warn_fun_requires_lock; |
| 1648 | break; |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1649 | case POK_PassByRef: |
| 1650 | DiagID = diag::warn_guarded_pass_by_reference; |
| 1651 | break; |
| 1652 | case POK_PtPassByRef: |
| 1653 | DiagID = diag::warn_pt_guarded_pass_by_reference; |
| 1654 | break; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1655 | } |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1656 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind |
| 1657 | << D->getNameAsString() |
| 1658 | << LockName << LK); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1659 | if (Verbose && POK == POK_VarAccess) { |
| 1660 | PartialDiagnosticAt Note(D->getLocation(), |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1661 | S.PDiag(diag::note_guarded_by_declared_here) |
| 1662 | << D->getNameAsString()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1663 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1664 | } else |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1665 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1666 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1669 | void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg, |
| 1670 | SourceLocation Loc) override { |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1671 | PartialDiagnosticAt Warning(Loc, |
| 1672 | S.PDiag(diag::warn_acquire_requires_negative_cap) |
| 1673 | << Kind << LockName << Neg); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1674 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1677 | void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1678 | SourceLocation Loc) override { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1679 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex) |
| 1680 | << Kind << FunName << LockName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1681 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1684 | void handleLockAcquiredBefore(StringRef Kind, Name L1Name, Name L2Name, |
| 1685 | SourceLocation Loc) override { |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1686 | PartialDiagnosticAt Warning(Loc, |
| 1687 | S.PDiag(diag::warn_acquired_before) << Kind << L1Name << L2Name); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1688 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1691 | void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) override { |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1692 | PartialDiagnosticAt Warning(Loc, |
| 1693 | S.PDiag(diag::warn_acquired_before_after_cycle) << L1Name); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1694 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1697 | void enterFunction(const FunctionDecl* FD) override { |
| 1698 | CurrentFunction = FD; |
| 1699 | } |
| 1700 | |
| 1701 | void leaveFunction(const FunctionDecl* FD) override { |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1702 | CurrentFunction = nullptr; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1703 | } |
| 1704 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1705 | } // anonymous namespace |
Benjamin Kramer | 539803c | 2015-03-19 14:23:45 +0000 | [diff] [blame] | 1706 | } // namespace threadSafety |
| 1707 | } // namespace clang |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1708 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1709 | //===----------------------------------------------------------------------===// |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1710 | // -Wconsumed |
| 1711 | //===----------------------------------------------------------------------===// |
| 1712 | |
| 1713 | namespace clang { |
| 1714 | namespace consumed { |
| 1715 | namespace { |
| 1716 | class ConsumedWarningsHandler : public ConsumedWarningsHandlerBase { |
| 1717 | |
| 1718 | Sema &S; |
| 1719 | DiagList Warnings; |
| 1720 | |
| 1721 | public: |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1722 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1723 | ConsumedWarningsHandler(Sema &S) : S(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1724 | |
| 1725 | void emitDiagnostics() override { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1726 | Warnings.sort(SortDiagBySourceLocation(S.getSourceManager())); |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1727 | for (const auto &Diag : Warnings) { |
| 1728 | S.Diag(Diag.first.first, Diag.first.second); |
| 1729 | for (const auto &Note : Diag.second) |
| 1730 | S.Diag(Note.first, Note.second); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1731 | } |
| 1732 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1733 | |
| 1734 | void warnLoopStateMismatch(SourceLocation Loc, |
| 1735 | StringRef VariableName) override { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1736 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_loop_state_mismatch) << |
| 1737 | VariableName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1738 | |
| 1739 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1742 | void warnParamReturnTypestateMismatch(SourceLocation Loc, |
| 1743 | StringRef VariableName, |
| 1744 | StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1745 | StringRef ObservedState) override { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1746 | |
| 1747 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1748 | diag::warn_param_return_typestate_mismatch) << VariableName << |
| 1749 | ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1750 | |
| 1751 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1754 | void warnParamTypestateMismatch(SourceLocation Loc, StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1755 | StringRef ObservedState) override { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1756 | |
| 1757 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1758 | diag::warn_param_typestate_mismatch) << ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1759 | |
| 1760 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1763 | void warnReturnTypestateForUnconsumableType(SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1764 | StringRef TypeName) override { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1765 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1766 | diag::warn_return_typestate_for_unconsumable_type) << TypeName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1767 | |
| 1768 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | void warnReturnTypestateMismatch(SourceLocation Loc, StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1772 | StringRef ObservedState) override { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1773 | |
| 1774 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1775 | diag::warn_return_typestate_mismatch) << ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1776 | |
| 1777 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1780 | void warnUseOfTempInInvalidState(StringRef MethodName, StringRef State, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1781 | SourceLocation Loc) override { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1782 | |
| 1783 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1784 | diag::warn_use_of_temp_in_invalid_state) << MethodName << State); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1785 | |
| 1786 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1789 | void warnUseInInvalidState(StringRef MethodName, StringRef VariableName, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1790 | StringRef State, SourceLocation Loc) override { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1791 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1792 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_in_invalid_state) << |
| 1793 | MethodName << VariableName << State); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1794 | |
| 1795 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1796 | } |
| 1797 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1798 | } // anonymous namespace |
| 1799 | } // namespace consumed |
| 1800 | } // namespace clang |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1801 | |
| 1802 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1803 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 1804 | // warnings on a function, method, or block. |
| 1805 | //===----------------------------------------------------------------------===// |
| 1806 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1807 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 1808 | enableCheckFallThrough = 1; |
| 1809 | enableCheckUnreachable = 0; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1810 | enableThreadSafetyAnalysis = 0; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1811 | enableConsumedAnalysis = 0; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1814 | static unsigned isEnabled(DiagnosticsEngine &D, unsigned diag) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1815 | return (unsigned)!D.isIgnored(diag, SourceLocation()); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1818 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) |
| 1819 | : S(s), |
| 1820 | NumFunctionsAnalyzed(0), |
Benjamin Kramer | 581f48f | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1821 | NumFunctionsWithBadCFGs(0), |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1822 | NumCFGBlocks(0), |
Benjamin Kramer | 581f48f | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1823 | MaxCFGBlocksPerFunction(0), |
| 1824 | NumUninitAnalysisFunctions(0), |
| 1825 | NumUninitAnalysisVariables(0), |
| 1826 | MaxUninitAnalysisVariablesPerFunction(0), |
| 1827 | NumUninitAnalysisBlockVisits(0), |
| 1828 | MaxUninitAnalysisBlockVisitsPerFunction(0) { |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1829 | |
| 1830 | using namespace diag; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1831 | DiagnosticsEngine &D = S.getDiagnostics(); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1832 | |
| 1833 | DefaultPolicy.enableCheckUnreachable = |
| 1834 | isEnabled(D, warn_unreachable) || |
| 1835 | isEnabled(D, warn_unreachable_break) || |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 1836 | isEnabled(D, warn_unreachable_return) || |
| 1837 | isEnabled(D, warn_unreachable_loop_increment); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1838 | |
| 1839 | DefaultPolicy.enableThreadSafetyAnalysis = |
| 1840 | isEnabled(D, warn_double_lock); |
| 1841 | |
| 1842 | DefaultPolicy.enableConsumedAnalysis = |
| 1843 | isEnabled(D, warn_use_in_invalid_state); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1846 | static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) { |
| 1847 | for (const auto &D : fscope->PossiblyUnreachableDiags) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1848 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1851 | void clang::sema:: |
| 1852 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 1853 | sema::FunctionScopeInfo *fscope, |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1854 | const Decl *D, const BlockExpr *blkExpr) { |
Ted Kremenek | b45ebee | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 1855 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1856 | // We avoid doing analysis-based warnings when there are errors for |
| 1857 | // two reasons: |
| 1858 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 1859 | // don't bother trying. |
| 1860 | // (2) The code already has problems; running the analysis just takes more |
| 1861 | // time. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1862 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | b802192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1863 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1864 | // Do not do any analysis for declarations in system headers if we are |
| 1865 | // going to just ignore them. |
Ted Kremenek | b802192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1866 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1867 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 1868 | return; |
| 1869 | |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 1870 | // For code in dependent contexts, we'll do this at instantiation time. |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1871 | if (cast<DeclContext>(D)->isDependentContext()) |
| 1872 | return; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1873 | |
DeLesley Hutchins | 8ecd491 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 1874 | if (Diags.hasUncompilableErrorOccurred() || Diags.hasFatalErrorOccurred()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1875 | // Flush out any possibly unreachable diagnostics. |
| 1876 | flushDiagnostics(S, fscope); |
| 1877 | return; |
| 1878 | } |
| 1879 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1880 | const Stmt *Body = D->getBody(); |
| 1881 | assert(Body); |
| 1882 | |
Ted Kremenek | b3a38a9 | 2013-10-14 19:11:25 +0000 | [diff] [blame] | 1883 | // Construct the analysis context with the specified CFG build options. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1884 | AnalysisDeclContext AC(/* AnalysisDeclContextManager */ nullptr, D); |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1885 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1886 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 1887 | // explosion for destructors that can result and the compile time hit. |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1888 | AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true; |
| 1889 | AC.getCFGBuildOptions().AddEHEdges = false; |
| 1890 | AC.getCFGBuildOptions().AddInitializers = true; |
| 1891 | AC.getCFGBuildOptions().AddImplicitDtors = true; |
Jordan Rose | 91f7840 | 2012-09-05 23:11:06 +0000 | [diff] [blame] | 1892 | AC.getCFGBuildOptions().AddTemporaryDtors = true; |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 1893 | AC.getCFGBuildOptions().AddCXXNewAllocator = false; |
Enrico Pertoso | faed801 | 2015-06-03 10:12:40 +0000 | [diff] [blame] | 1894 | AC.getCFGBuildOptions().AddCXXDefaultInitExprInCtors = true; |
Jordan Rose | 91f7840 | 2012-09-05 23:11:06 +0000 | [diff] [blame] | 1895 | |
Ted Kremenek | 9e100ea | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 1896 | // Force that certain expressions appear as CFGElements in the CFG. This |
| 1897 | // is used to speed up various analyses. |
| 1898 | // FIXME: This isn't the right factoring. This is here for initial |
| 1899 | // prototyping, but we need a way for analyses to say what expressions they |
| 1900 | // expect to always be CFGElements and then fill in the BuildOptions |
| 1901 | // appropriately. This is essentially a layering violation. |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1902 | if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis || |
| 1903 | P.enableConsumedAnalysis) { |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1904 | // Unreachable code analysis and thread safety require a linearized CFG. |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1905 | AC.getCFGBuildOptions().setAllAlwaysAdd(); |
| 1906 | } |
| 1907 | else { |
| 1908 | AC.getCFGBuildOptions() |
| 1909 | .setAlwaysAdd(Stmt::BinaryOperatorClass) |
Richard Smith | b21dd02 | 2012-07-17 01:27:33 +0000 | [diff] [blame] | 1910 | .setAlwaysAdd(Stmt::CompoundAssignOperatorClass) |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1911 | .setAlwaysAdd(Stmt::BlockExprClass) |
| 1912 | .setAlwaysAdd(Stmt::CStyleCastExprClass) |
| 1913 | .setAlwaysAdd(Stmt::DeclRefExprClass) |
| 1914 | .setAlwaysAdd(Stmt::ImplicitCastExprClass) |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1915 | .setAlwaysAdd(Stmt::UnaryOperatorClass) |
| 1916 | .setAlwaysAdd(Stmt::AttributedStmtClass); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1917 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1918 | |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 1919 | // Install the logical handler for -Wtautological-overlap-compare |
| 1920 | std::unique_ptr<LogicalErrorHandler> LEH; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1921 | if (!Diags.isIgnored(diag::warn_tautological_overlap_comparison, |
| 1922 | D->getLocStart())) { |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 1923 | LEH.reset(new LogicalErrorHandler(S)); |
| 1924 | AC.getCFGBuildOptions().Observer = LEH.get(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 1925 | } |
Ted Kremenek | b3a38a9 | 2013-10-14 19:11:25 +0000 | [diff] [blame] | 1926 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1927 | // Emit delayed diagnostics. |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1928 | if (!fscope->PossiblyUnreachableDiags.empty()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1929 | bool analyzed = false; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Register the expressions with the CFGBuilder. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1932 | for (const auto &D : fscope->PossiblyUnreachableDiags) { |
| 1933 | if (D.stmt) |
| 1934 | AC.registerForcedBlockExpression(D.stmt); |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | if (AC.getCFG()) { |
| 1938 | analyzed = true; |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1939 | for (const auto &D : fscope->PossiblyUnreachableDiags) { |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1940 | bool processed = false; |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1941 | if (D.stmt) { |
| 1942 | const CFGBlock *block = AC.getBlockForRegisteredExpression(D.stmt); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 1943 | CFGReverseBlockReachabilityAnalysis *cra = |
| 1944 | AC.getCFGReachablityAnalysis(); |
| 1945 | // FIXME: We should be able to assert that block is non-null, but |
| 1946 | // the CFG analysis can skip potentially-evaluated expressions in |
| 1947 | // edge cases; see test/Sema/vla-2.c. |
| 1948 | if (block && cra) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1949 | // Can this block be reached from the entrance? |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1950 | if (cra->isReachable(&AC.getCFG()->getEntry(), block)) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1951 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1952 | processed = true; |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1955 | if (!processed) { |
| 1956 | // Emit the warning anyway if we cannot map to a basic block. |
| 1957 | S.Diag(D.Loc, D.PD); |
| 1958 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1959 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1960 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1961 | |
| 1962 | if (!analyzed) |
| 1963 | flushDiagnostics(S, fscope); |
| 1964 | } |
| 1965 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1966 | // Warning: check missing 'return' |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1967 | if (P.enableCheckFallThrough) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1968 | const CheckFallThroughDiagnostics &CD = |
| 1969 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 1970 | : (isa<CXXMethodDecl>(D) && |
| 1971 | cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call && |
| 1972 | cast<CXXMethodDecl>(D)->getParent()->isLambda()) |
| 1973 | ? CheckFallThroughDiagnostics::MakeForLambda() |
| 1974 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1975 | CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | // Warning: check for unreachable code |
Ted Kremenek | 7f77003 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 1979 | if (P.enableCheckUnreachable) { |
| 1980 | // Only check for unreachable code on non-template instantiations. |
| 1981 | // Different template instantiations can effectively change the control-flow |
| 1982 | // and it is very difficult to prove that a snippet of code in a template |
| 1983 | // is unreachable for all instantiations. |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 1984 | bool isTemplateInstantiation = false; |
| 1985 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 1986 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
| 1987 | if (!isTemplateInstantiation) |
Ted Kremenek | 7f77003 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 1988 | CheckUnreachable(S, AC); |
| 1989 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1990 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1991 | // Check for thread safety violations |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1992 | if (P.enableThreadSafetyAnalysis) { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1993 | SourceLocation FL = AC.getDecl()->getLocation(); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1994 | SourceLocation FEL = AC.getDecl()->getLocEnd(); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1995 | threadSafety::ThreadSafetyReporter Reporter(S, FL, FEL); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1996 | if (!Diags.isIgnored(diag::warn_thread_safety_beta, D->getLocStart())) |
DeLesley Hutchins | 8edae13 | 2012-12-05 00:06:15 +0000 | [diff] [blame] | 1997 | Reporter.setIssueBetaWarnings(true); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1998 | if (!Diags.isIgnored(diag::warn_thread_safety_verbose, D->getLocStart())) |
| 1999 | Reporter.setVerbose(true); |
DeLesley Hutchins | 8edae13 | 2012-12-05 00:06:15 +0000 | [diff] [blame] | 2000 | |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 2001 | threadSafety::runThreadSafetyAnalysis(AC, Reporter, |
| 2002 | &S.ThreadSafetyDeclCache); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 2003 | Reporter.emitDiagnostics(); |
| 2004 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 2005 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 2006 | // Check for violations of consumed properties. |
| 2007 | if (P.enableConsumedAnalysis) { |
| 2008 | consumed::ConsumedWarningsHandler WarningHandler(S); |
Reid Kleckner | e846dea | 2013-08-12 23:49:39 +0000 | [diff] [blame] | 2009 | consumed::ConsumedAnalyzer Analyzer(WarningHandler); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 2010 | Analyzer.run(AC); |
| 2011 | } |
| 2012 | |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2013 | if (!Diags.isIgnored(diag::warn_uninit_var, D->getLocStart()) || |
| 2014 | !Diags.isIgnored(diag::warn_sometimes_uninit_var, D->getLocStart()) || |
| 2015 | !Diags.isIgnored(diag::warn_maybe_uninit_var, D->getLocStart())) { |
Ted Kremenek | 2551fbe | 2011-03-17 05:29:57 +0000 | [diff] [blame] | 2016 | if (CFG *cfg = AC.getCFG()) { |
Ted Kremenek | b63931e | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 2017 | UninitValsDiagReporter reporter(S); |
Fariborz Jahanian | 8809a9d | 2011-07-16 18:31:33 +0000 | [diff] [blame] | 2018 | UninitVariablesAnalysisStats stats; |
Benjamin Kramer | e492cb4 | 2011-07-16 20:13:06 +0000 | [diff] [blame] | 2019 | std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats)); |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 2020 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2021 | reporter, stats); |
| 2022 | |
| 2023 | if (S.CollectStats && stats.NumVariablesAnalyzed > 0) { |
| 2024 | ++NumUninitAnalysisFunctions; |
| 2025 | NumUninitAnalysisVariables += stats.NumVariablesAnalyzed; |
| 2026 | NumUninitAnalysisBlockVisits += stats.NumBlockVisits; |
| 2027 | MaxUninitAnalysisVariablesPerFunction = |
| 2028 | std::max(MaxUninitAnalysisVariablesPerFunction, |
| 2029 | stats.NumVariablesAnalyzed); |
| 2030 | MaxUninitAnalysisBlockVisitsPerFunction = |
| 2031 | std::max(MaxUninitAnalysisBlockVisitsPerFunction, |
| 2032 | stats.NumBlockVisits); |
| 2033 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2036 | |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 2037 | bool FallThroughDiagFull = |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2038 | !Diags.isIgnored(diag::warn_unannotated_fallthrough, D->getLocStart()); |
| 2039 | bool FallThroughDiagPerFunction = !Diags.isIgnored( |
| 2040 | diag::warn_unannotated_fallthrough_per_function, D->getLocStart()); |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 2041 | if (FallThroughDiagFull || FallThroughDiagPerFunction) { |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 2042 | DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2045 | if (S.getLangOpts().ObjCWeak && |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2046 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, D->getLocStart())) |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 2047 | diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap()); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 2048 | |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Check for infinite self-recursion in functions |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2051 | if (!Diags.isIgnored(diag::warn_infinite_recursive_function, |
| 2052 | D->getLocStart())) { |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 2053 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2054 | checkRecursiveFunction(S, FD, Body, AC); |
| 2055 | } |
| 2056 | } |
| 2057 | |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 2058 | // If none of the previous checks caused a CFG build, trigger one here |
| 2059 | // for -Wtautological-overlap-compare |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2060 | if (!Diags.isIgnored(diag::warn_tautological_overlap_comparison, |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 2061 | D->getLocStart())) { |
| 2062 | AC.getCFG(); |
| 2063 | } |
| 2064 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2065 | // Collect statistics about the CFG if it was built. |
| 2066 | if (S.CollectStats && AC.isCFGBuilt()) { |
| 2067 | ++NumFunctionsAnalyzed; |
| 2068 | if (CFG *cfg = AC.getCFG()) { |
| 2069 | // If we successfully built a CFG for this context, record some more |
| 2070 | // detail information about it. |
Chandler Carruth | 50020d9 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 2071 | NumCFGBlocks += cfg->getNumBlockIDs(); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2072 | MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction, |
Chandler Carruth | 50020d9 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 2073 | cfg->getNumBlockIDs()); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2074 | } else { |
| 2075 | ++NumFunctionsWithBadCFGs; |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | void clang::sema::AnalysisBasedWarnings::PrintStats() const { |
| 2081 | llvm::errs() << "\n*** Analysis Based Warnings Stats:\n"; |
| 2082 | |
| 2083 | unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs; |
| 2084 | unsigned AvgCFGBlocksPerFunction = |
| 2085 | !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt; |
| 2086 | llvm::errs() << NumFunctionsAnalyzed << " functions analyzed (" |
| 2087 | << NumFunctionsWithBadCFGs << " w/o CFGs).\n" |
| 2088 | << " " << NumCFGBlocks << " CFG blocks built.\n" |
| 2089 | << " " << AvgCFGBlocksPerFunction |
| 2090 | << " average CFG blocks per function.\n" |
| 2091 | << " " << MaxCFGBlocksPerFunction |
| 2092 | << " max CFG blocks per function.\n"; |
| 2093 | |
| 2094 | unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 2095 | : NumUninitAnalysisVariables/NumUninitAnalysisFunctions; |
| 2096 | unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 2097 | : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions; |
| 2098 | llvm::errs() << NumUninitAnalysisFunctions |
| 2099 | << " functions analyzed for uninitialiazed variables\n" |
| 2100 | << " " << NumUninitAnalysisVariables << " variables analyzed.\n" |
| 2101 | << " " << AvgUninitVariablesPerFunction |
| 2102 | << " average variables per function.\n" |
| 2103 | << " " << MaxUninitAnalysisVariablesPerFunction |
| 2104 | << " max variables per function.\n" |
| 2105 | << " " << NumUninitAnalysisBlockVisits << " block visits.\n" |
| 2106 | << " " << AvgUninitBlockVisitsPerFunction |
| 2107 | << " average block visits per function.\n" |
| 2108 | << " " << MaxUninitAnalysisBlockVisitsPerFunction |
| 2109 | << " max block visits per function.\n"; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2110 | } |