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