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