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