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