Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1 | //=- AnalysisBasedWarnings.cpp - Sema warnings based on libAnalysis -*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines analysis_warnings::[Policy,Executor]. |
| 11 | // Together they are used by Sema to issue warnings based on inexpensive |
| 12 | // static analysis algorithms in libAnalysis. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/AnalysisBasedWarnings.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceLocation.h" |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
| 25 | #include "clang/AST/ExprCXX.h" |
| 26 | #include "clang/AST/StmtObjC.h" |
| 27 | #include "clang/AST/StmtCXX.h" |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 28 | #include "clang/AST/EvaluatedExprVisitor.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 29 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 30 | #include "clang/Analysis/AnalysisContext.h" |
| 31 | #include "clang/Analysis/CFG.h" |
| 32 | #include "clang/Analysis/Analyses/ReachableCode.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 33 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
| 34 | #include "clang/Analysis/CFGStmtMap.h" |
Ted Kremenek | 6f34213 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 35 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/BitVector.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/FoldingSet.h" |
| 38 | #include "llvm/ADT/ImmutableMap.h" |
| 39 | #include "llvm/ADT/PostOrderIterator.h" |
| 40 | #include "llvm/ADT/SmallVector.h" |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringRef.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Casting.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 43 | #include <algorithm> |
| 44 | #include <vector> |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 45 | |
| 46 | using namespace clang; |
| 47 | |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | // Unreachable code analysis. |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | namespace { |
| 53 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 54 | Sema &S; |
| 55 | public: |
| 56 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 57 | |
| 58 | void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) { |
| 59 | S.Diag(L, diag::warn_unreachable) << R1 << R2; |
| 60 | } |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | /// CheckUnreachable - Check for unreachable code. |
| 65 | static void CheckUnreachable(Sema &S, AnalysisContext &AC) { |
| 66 | UnreachableCodeHandler UC(S); |
| 67 | reachable_code::FindUnreachableCode(AC, UC); |
| 68 | } |
| 69 | |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | // Check for missing return value. |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 74 | enum ControlFlowKind { |
| 75 | UnknownFallThrough, |
| 76 | NeverFallThrough, |
| 77 | MaybeFallThrough, |
| 78 | AlwaysFallThrough, |
| 79 | NeverFallThroughOrReturn |
| 80 | }; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 81 | |
| 82 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 83 | /// Statement that should return a value. |
| 84 | /// |
| 85 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 86 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 87 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 88 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
| 89 | /// statement but we may return. We assume that functions not marked noreturn |
| 90 | /// will return. |
| 91 | static ControlFlowKind CheckFallThrough(AnalysisContext &AC) { |
| 92 | CFG *cfg = AC.getCFG(); |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 93 | if (cfg == 0) return UnknownFallThrough; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 94 | |
| 95 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 96 | // confuse us, so we mark all live things first. |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 97 | llvm::BitVector live(cfg->getNumBlockIDs()); |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 98 | unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(), |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 99 | live); |
| 100 | |
| 101 | bool AddEHEdges = AC.getAddEHEdges(); |
| 102 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 103 | // When there are things remaining dead, and we didn't add EH edges |
| 104 | // from CallExprs to the catch clauses, we have to go back and |
| 105 | // mark them as live. |
| 106 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
| 107 | CFGBlock &b = **I; |
| 108 | if (!live[b.getBlockID()]) { |
| 109 | if (b.pred_begin() == b.pred_end()) { |
| 110 | if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator())) |
| 111 | // When not adding EH edges from calls, catch clauses |
| 112 | // can otherwise seem dead. Avoid noting them as dead. |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 113 | count += reachable_code::ScanReachableFromBlock(&b, live); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Now we know what is live, we check the live precessors of the exit block |
| 120 | // and look for fall through paths, being careful to ignore normal returns, |
| 121 | // and exceptional paths. |
| 122 | bool HasLiveReturn = false; |
| 123 | bool HasFakeEdge = false; |
| 124 | bool HasPlainEdge = false; |
| 125 | bool HasAbnormalEdge = false; |
Ted Kremenek | 90b828a | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 126 | |
| 127 | // Ignore default cases that aren't likely to be reachable because all |
| 128 | // enums in a switch(X) have explicit case statements. |
| 129 | CFGBlock::FilterOptions FO; |
| 130 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 131 | |
| 132 | for (CFGBlock::filtered_pred_iterator |
| 133 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { |
| 134 | const CFGBlock& B = **I; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 135 | if (!live[B.getBlockID()]) |
| 136 | continue; |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 137 | |
| 138 | // Destructors can appear after the 'return' in the CFG. This is |
| 139 | // normal. We need to look pass the destructors for the return |
| 140 | // statement (if it exists). |
| 141 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 142 | bool hasNoReturnDtor = false; |
| 143 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 144 | for ( ; ri != re ; ++ri) { |
| 145 | CFGElement CE = *ri; |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 146 | |
| 147 | // FIXME: The right solution is to just sever the edges in the |
| 148 | // CFG itself. |
| 149 | if (const CFGImplicitDtor *iDtor = ri->getAs<CFGImplicitDtor>()) |
Ted Kremenek | c5aff44 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 150 | if (iDtor->isNoReturn(AC.getASTContext())) { |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 151 | hasNoReturnDtor = true; |
| 152 | HasFakeEdge = true; |
| 153 | break; |
| 154 | } |
| 155 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 156 | if (isa<CFGStmt>(CE)) |
| 157 | break; |
| 158 | } |
| 159 | |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 160 | if (hasNoReturnDtor) |
| 161 | continue; |
| 162 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 163 | // No more CFGElements in the block? |
| 164 | if (ri == re) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 165 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 166 | HasAbnormalEdge = true; |
| 167 | continue; |
| 168 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 169 | // A labeled empty statement, or the entry block... |
| 170 | HasPlainEdge = true; |
| 171 | continue; |
| 172 | } |
Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 174 | CFGStmt CS = cast<CFGStmt>(*ri); |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 175 | const Stmt *S = CS.getStmt(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 176 | if (isa<ReturnStmt>(S)) { |
| 177 | HasLiveReturn = true; |
| 178 | continue; |
| 179 | } |
| 180 | if (isa<ObjCAtThrowStmt>(S)) { |
| 181 | HasFakeEdge = true; |
| 182 | continue; |
| 183 | } |
| 184 | if (isa<CXXThrowExpr>(S)) { |
| 185 | HasFakeEdge = true; |
| 186 | continue; |
| 187 | } |
| 188 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { |
| 189 | if (AS->isMSAsm()) { |
| 190 | HasFakeEdge = true; |
| 191 | HasLiveReturn = true; |
| 192 | continue; |
| 193 | } |
| 194 | } |
| 195 | if (isa<CXXTryStmt>(S)) { |
| 196 | HasAbnormalEdge = true; |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | bool NoReturnEdge = false; |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 201 | if (const CallExpr *C = dyn_cast<CallExpr>(S)) { |
John McCall | 259d48e | 2010-04-30 07:10:06 +0000 | [diff] [blame] | 202 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 203 | == B.succ_end()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 204 | HasAbnormalEdge = true; |
| 205 | continue; |
| 206 | } |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 207 | const Expr *CEE = C->getCallee()->IgnoreParenCasts(); |
John McCall | 1de8533 | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 208 | QualType calleeType = CEE->getType(); |
| 209 | if (calleeType == AC.getASTContext().BoundMemberTy) { |
| 210 | calleeType = Expr::findBoundMemberType(CEE); |
| 211 | assert(!calleeType.isNull() && "analyzing unresolved call?"); |
| 212 | } |
| 213 | if (getFunctionExtInfo(calleeType).getNoReturn()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 214 | NoReturnEdge = true; |
| 215 | HasFakeEdge = true; |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 216 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 217 | const ValueDecl *VD = DRE->getDecl(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 218 | if (VD->hasAttr<NoReturnAttr>()) { |
| 219 | NoReturnEdge = true; |
| 220 | HasFakeEdge = true; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | // FIXME: Add noreturn message sends. |
| 225 | if (NoReturnEdge == false) |
| 226 | HasPlainEdge = true; |
| 227 | } |
| 228 | if (!HasPlainEdge) { |
| 229 | if (HasLiveReturn) |
| 230 | return NeverFallThrough; |
| 231 | return NeverFallThroughOrReturn; |
| 232 | } |
| 233 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 234 | return MaybeFallThrough; |
| 235 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 236 | // noreturn, that don't return. If people would like this warning to be more |
| 237 | // accurate, such functions should be marked as noreturn. |
| 238 | return AlwaysFallThrough; |
| 239 | } |
| 240 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 241 | namespace { |
| 242 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 243 | struct CheckFallThroughDiagnostics { |
| 244 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 245 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 246 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 247 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 248 | unsigned diag_NeverFallThroughOrReturn; |
| 249 | bool funMode; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 250 | SourceLocation FuncLoc; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 252 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 253 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 254 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 255 | D.diag_MaybeFallThrough_HasNoReturn = |
| 256 | diag::warn_falloff_noreturn_function; |
| 257 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 258 | diag::warn_maybe_falloff_nonvoid_function; |
| 259 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 260 | diag::warn_falloff_noreturn_function; |
| 261 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 262 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 263 | |
| 264 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 265 | // might be overridden by non-noreturn functions. |
| 266 | bool isVirtualMethod = false; |
| 267 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 268 | isVirtualMethod = Method->isVirtual(); |
| 269 | |
| 270 | if (!isVirtualMethod) |
| 271 | D.diag_NeverFallThroughOrReturn = |
| 272 | diag::warn_suggest_noreturn_function; |
| 273 | else |
| 274 | D.diag_NeverFallThroughOrReturn = 0; |
| 275 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 276 | D.funMode = true; |
| 277 | return D; |
| 278 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 280 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 281 | CheckFallThroughDiagnostics D; |
| 282 | D.diag_MaybeFallThrough_HasNoReturn = |
| 283 | diag::err_noreturn_block_has_return_expr; |
| 284 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 285 | diag::err_maybe_falloff_nonvoid_block; |
| 286 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 287 | diag::err_noreturn_block_has_return_expr; |
| 288 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 289 | diag::err_falloff_nonvoid_block; |
| 290 | D.diag_NeverFallThroughOrReturn = |
| 291 | diag::warn_suggest_noreturn_block; |
| 292 | D.funMode = false; |
| 293 | return D; |
| 294 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 296 | bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid, |
| 297 | bool HasNoReturn) const { |
| 298 | if (funMode) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 299 | return (ReturnsVoid || |
| 300 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, |
| 301 | FuncLoc) == Diagnostic::Ignored) |
| 302 | && (!HasNoReturn || |
| 303 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, |
| 304 | FuncLoc) == Diagnostic::Ignored) |
| 305 | && (!ReturnsVoid || |
| 306 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 307 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 308 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 310 | // For blocks. |
| 311 | return ReturnsVoid && !HasNoReturn |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 312 | && (!ReturnsVoid || |
| 313 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 314 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 320 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 321 | /// function that should return a value. Check that we don't fall off the end |
| 322 | /// of a noreturn function. We assume that functions and blocks not marked |
| 323 | /// noreturn will return. |
| 324 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 325 | const BlockExpr *blkExpr, |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 326 | const CheckFallThroughDiagnostics& CD, |
| 327 | AnalysisContext &AC) { |
| 328 | |
| 329 | bool ReturnsVoid = false; |
| 330 | bool HasNoReturn = false; |
| 331 | |
| 332 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 333 | ReturnsVoid = FD->getResultType()->isVoidType(); |
| 334 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 335 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 336 | } |
| 337 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 338 | ReturnsVoid = MD->getResultType()->isVoidType(); |
| 339 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 340 | } |
| 341 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 342 | QualType BlockTy = blkExpr->getType(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 343 | if (const FunctionType *FT = |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 344 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
| 345 | if (FT->getResultType()->isVoidType()) |
| 346 | ReturnsVoid = true; |
| 347 | if (FT->getNoReturnAttr()) |
| 348 | HasNoReturn = true; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | Diagnostic &Diags = S.getDiagnostics(); |
| 353 | |
| 354 | // Short circuit for compilation speed. |
| 355 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 356 | return; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 358 | // FIXME: Function try block |
| 359 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { |
| 360 | switch (CheckFallThrough(AC)) { |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 361 | case UnknownFallThrough: |
| 362 | break; |
| 363 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 364 | case MaybeFallThrough: |
| 365 | if (HasNoReturn) |
| 366 | S.Diag(Compound->getRBracLoc(), |
| 367 | CD.diag_MaybeFallThrough_HasNoReturn); |
| 368 | else if (!ReturnsVoid) |
| 369 | S.Diag(Compound->getRBracLoc(), |
| 370 | CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 371 | break; |
| 372 | case AlwaysFallThrough: |
| 373 | if (HasNoReturn) |
| 374 | S.Diag(Compound->getRBracLoc(), |
| 375 | CD.diag_AlwaysFallThrough_HasNoReturn); |
| 376 | else if (!ReturnsVoid) |
| 377 | S.Diag(Compound->getRBracLoc(), |
| 378 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 379 | break; |
| 380 | case NeverFallThroughOrReturn: |
Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 381 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) { |
| 382 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 383 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn) |
| 384 | << FD; |
| 385 | } else { |
| 386 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn); |
| 387 | } |
| 388 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 389 | break; |
| 390 | case NeverFallThrough: |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 397 | // -Wuninitialized |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 400 | namespace { |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 401 | /// ContainsReference - A visitor class to search for references to |
| 402 | /// a particular declaration (the needle) within any evaluated component of an |
| 403 | /// expression (recursively). |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 404 | class ContainsReference : public EvaluatedExprVisitor<ContainsReference> { |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 405 | bool FoundReference; |
| 406 | const DeclRefExpr *Needle; |
| 407 | |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 408 | public: |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 409 | ContainsReference(ASTContext &Context, const DeclRefExpr *Needle) |
| 410 | : EvaluatedExprVisitor<ContainsReference>(Context), |
| 411 | FoundReference(false), Needle(Needle) {} |
| 412 | |
| 413 | void VisitExpr(Expr *E) { |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 414 | // Stop evaluating if we already have a reference. |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 415 | if (FoundReference) |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 416 | return; |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 417 | |
| 418 | EvaluatedExprVisitor<ContainsReference>::VisitExpr(E); |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 419 | } |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 420 | |
| 421 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 422 | if (E == Needle) |
| 423 | FoundReference = true; |
| 424 | else |
| 425 | EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(E); |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 426 | } |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 427 | |
| 428 | bool doesContainReference() const { return FoundReference; } |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 429 | }; |
| 430 | } |
| 431 | |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 432 | /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an |
| 433 | /// uninitialized variable. This manages the different forms of diagnostic |
| 434 | /// emitted for particular types of uses. Returns true if the use was diagnosed |
| 435 | /// as a warning. If a pariticular use is one we omit warnings for, returns |
| 436 | /// false. |
| 437 | static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD, |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 438 | const Expr *E, bool isAlwaysUninit) { |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 439 | bool isSelfInit = false; |
| 440 | |
| 441 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 442 | if (isAlwaysUninit) { |
| 443 | // Inspect the initializer of the variable declaration which is |
| 444 | // being referenced prior to its initialization. We emit |
| 445 | // specialized diagnostics for self-initialization, and we |
| 446 | // specifically avoid warning about self references which take the |
| 447 | // form of: |
| 448 | // |
| 449 | // int x = x; |
| 450 | // |
| 451 | // This is used to indicate to GCC that 'x' is intentionally left |
| 452 | // uninitialized. Proven code paths which access 'x' in |
| 453 | // an uninitialized state after this will still warn. |
| 454 | // |
| 455 | // TODO: Should we suppress maybe-uninitialized warnings for |
| 456 | // variables initialized in this way? |
| 457 | if (const Expr *Initializer = VD->getInit()) { |
| 458 | if (DRE == Initializer->IgnoreParenImpCasts()) |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 459 | return false; |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 460 | |
| 461 | ContainsReference CR(S.Context, DRE); |
| 462 | CR.Visit(const_cast<Expr*>(Initializer)); |
| 463 | isSelfInit = CR.doesContainReference(); |
| 464 | } |
| 465 | if (isSelfInit) { |
| 466 | S.Diag(DRE->getLocStart(), |
| 467 | diag::warn_uninit_self_reference_in_init) |
| 468 | << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange(); |
| 469 | } else { |
| 470 | S.Diag(DRE->getLocStart(), diag::warn_uninit_var) |
| 471 | << VD->getDeclName() << DRE->getSourceRange(); |
| 472 | } |
| 473 | } else { |
| 474 | S.Diag(DRE->getLocStart(), diag::warn_maybe_uninit_var) |
| 475 | << VD->getDeclName() << DRE->getSourceRange(); |
| 476 | } |
| 477 | } else { |
| 478 | const BlockExpr *BE = cast<BlockExpr>(E); |
| 479 | S.Diag(BE->getLocStart(), |
| 480 | isAlwaysUninit ? diag::warn_uninit_var_captured_by_block |
| 481 | : diag::warn_maybe_uninit_var_captured_by_block) |
| 482 | << VD->getDeclName(); |
| 483 | } |
| 484 | |
| 485 | // Report where the variable was declared when the use wasn't within |
| 486 | // the initializer of that declaration. |
| 487 | if (!isSelfInit) |
| 488 | S.Diag(VD->getLocStart(), diag::note_uninit_var_def) |
| 489 | << VD->getDeclName(); |
| 490 | |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 491 | return true; |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 494 | static void SuggestInitializationFixit(Sema &S, const VarDecl *VD) { |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 495 | // Don't issue a fixit if there is already an initializer. |
| 496 | if (VD->getInit()) |
| 497 | return; |
| 498 | |
| 499 | // Suggest possible initialization (if any). |
| 500 | const char *initialization = 0; |
| 501 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 502 | |
Douglas Gregor | 8ba4426 | 2011-07-02 00:59:18 +0000 | [diff] [blame] | 503 | if (VariableTy->isObjCObjectPointerType() || |
| 504 | VariableTy->isBlockPointerType()) { |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 505 | // Check if 'nil' is defined. |
| 506 | if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil"))) |
| 507 | initialization = " = nil"; |
| 508 | else |
| 509 | initialization = " = 0"; |
| 510 | } |
| 511 | else if (VariableTy->isRealFloatingType()) |
| 512 | initialization = " = 0.0"; |
| 513 | else if (VariableTy->isBooleanType() && S.Context.getLangOptions().CPlusPlus) |
| 514 | initialization = " = false"; |
| 515 | else if (VariableTy->isEnumeralType()) |
| 516 | return; |
Douglas Gregor | 8ba4426 | 2011-07-02 00:59:18 +0000 | [diff] [blame] | 517 | else if (VariableTy->isPointerType() || VariableTy->isMemberPointerType()) { |
Douglas Gregor | cc68c9b | 2011-08-27 00:18:50 +0000 | [diff] [blame] | 518 | if (S.Context.getLangOptions().CPlusPlus0x) |
| 519 | initialization = " = nullptr"; |
Douglas Gregor | 8ba4426 | 2011-07-02 00:59:18 +0000 | [diff] [blame] | 520 | // Check if 'NULL' is defined. |
Douglas Gregor | cc68c9b | 2011-08-27 00:18:50 +0000 | [diff] [blame] | 521 | else if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("NULL"))) |
Douglas Gregor | 8ba4426 | 2011-07-02 00:59:18 +0000 | [diff] [blame] | 522 | initialization = " = NULL"; |
| 523 | else |
| 524 | initialization = " = 0"; |
| 525 | } |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 526 | else if (VariableTy->isScalarType()) |
| 527 | initialization = " = 0"; |
| 528 | |
| 529 | if (initialization) { |
| 530 | SourceLocation loc = S.PP.getLocForEndOfToken(VD->getLocEnd()); |
| 531 | S.Diag(loc, diag::note_var_fixit_add_initialization) |
| 532 | << FixItHint::CreateInsertion(loc, initialization); |
| 533 | } |
| 534 | } |
| 535 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 536 | typedef std::pair<const Expr*, bool> UninitUse; |
| 537 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 538 | namespace { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 539 | struct SLocSort { |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 540 | bool operator()(const UninitUse &a, const UninitUse &b) { |
| 541 | SourceLocation aLoc = a.first->getLocStart(); |
| 542 | SourceLocation bLoc = b.first->getLocStart(); |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 543 | return aLoc.getRawEncoding() < bLoc.getRawEncoding(); |
| 544 | } |
| 545 | }; |
| 546 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 547 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 548 | Sema &S; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 549 | typedef SmallVector<UninitUse, 2> UsesVec; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 550 | typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap; |
| 551 | UsesMap *uses; |
| 552 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 553 | public: |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 554 | UninitValsDiagReporter(Sema &S) : S(S), uses(0) {} |
| 555 | ~UninitValsDiagReporter() { |
| 556 | flushDiagnostics(); |
| 557 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 559 | void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd, |
| 560 | bool isAlwaysUninit) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 561 | if (!uses) |
| 562 | uses = new UsesMap(); |
| 563 | |
| 564 | UsesVec *&vec = (*uses)[vd]; |
| 565 | if (!vec) |
| 566 | vec = new UsesVec(); |
| 567 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 568 | vec->push_back(std::make_pair(ex, isAlwaysUninit)); |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void flushDiagnostics() { |
| 572 | if (!uses) |
| 573 | return; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 574 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 575 | for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) { |
| 576 | const VarDecl *vd = i->first; |
| 577 | UsesVec *vec = i->second; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 578 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 579 | // Sort the uses by their SourceLocations. While not strictly |
| 580 | // guaranteed to produce them in line/column order, this will provide |
| 581 | // a stable ordering. |
| 582 | std::sort(vec->begin(), vec->end(), SLocSort()); |
| 583 | |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 584 | for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; |
| 585 | ++vi) { |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 586 | if (!DiagnoseUninitializedUse(S, vd, vi->first, |
| 587 | /*isAlwaysUninit=*/vi->second)) |
| 588 | continue; |
| 589 | |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 590 | SuggestInitializationFixit(S, vd); |
| 591 | |
| 592 | // Skip further diagnostics for this variable. We try to warn only on |
| 593 | // the first point at which a variable is used uninitialized. |
| 594 | break; |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 595 | } |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 597 | delete vec; |
| 598 | } |
| 599 | delete uses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 600 | } |
| 601 | }; |
| 602 | } |
| 603 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 604 | |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | // -Wthread-safety |
| 607 | //===----------------------------------------------------------------------===// |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 608 | namespace clang { |
| 609 | namespace thread_safety { |
| 610 | typedef std::pair<SourceLocation, PartialDiagnostic> DelayedDiag; |
| 611 | typedef llvm::SmallVector<DelayedDiag, 4> DiagList; |
| 612 | |
| 613 | enum ProtectedOperationKind { |
| 614 | POK_VarDereference, |
| 615 | POK_VarAccess, |
| 616 | POK_FunctionCall |
| 617 | }; |
| 618 | |
| 619 | enum LockKind { |
| 620 | LK_Shared, |
| 621 | LK_Exclusive |
| 622 | }; |
| 623 | |
| 624 | enum AccessKind { |
| 625 | AK_Read, |
| 626 | AK_Written |
| 627 | }; |
| 628 | |
| 629 | |
| 630 | struct SortDiagBySourceLocation { |
| 631 | Sema &S; |
| 632 | SortDiagBySourceLocation(Sema &S) : S(S) {} |
| 633 | |
| 634 | bool operator()(const DelayedDiag &left, const DelayedDiag &right) { |
| 635 | // Although this call will be slow, this is only called when outputting |
| 636 | // multiple warnings. |
| 637 | return S.getSourceManager().isBeforeInTranslationUnit(left.first, |
| 638 | right.first); |
| 639 | } |
| 640 | }; |
| 641 | |
| 642 | /// \brief Helper function that returns a LockKind required for the given level |
| 643 | /// of access. |
| 644 | LockKind getLockKindFromAccessKind(AccessKind AK) { |
| 645 | switch (AK) { |
| 646 | case AK_Read : |
| 647 | return LK_Shared; |
| 648 | case AK_Written : |
| 649 | return LK_Exclusive; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | class ThreadSafetyHandler { |
| 654 | public: |
| 655 | typedef llvm::StringRef Name; |
| 656 | ThreadSafetyHandler() {} |
| 657 | virtual ~ThreadSafetyHandler() {} |
| 658 | virtual void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {} |
| 659 | virtual void handleDoubleLock(Name LockName, SourceLocation Loc) {} |
| 660 | virtual void handleMutexHeldEndOfScope(Name LockName, SourceLocation Loc){} |
| 661 | virtual void handleNoLockLoopEntry(Name LockName, SourceLocation Loc) {} |
| 662 | virtual void handleNoUnlock(Name LockName, Name FunName, |
| 663 | SourceLocation Loc) {} |
| 664 | virtual void handleExclusiveAndShared(Name LockName, SourceLocation Loc1, |
| 665 | SourceLocation Loc2) {} |
| 666 | virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, |
| 667 | AccessKind AK, SourceLocation Loc) {} |
| 668 | virtual void handleMutexNotHeld(const NamedDecl *D, |
| 669 | ProtectedOperationKind POK, Name LockName, |
| 670 | LockKind LK, SourceLocation Loc) {} |
| 671 | virtual void handleFunExcludesLock(Name FunName, Name LockName, |
| 672 | SourceLocation Loc) {} |
| 673 | }; |
| 674 | |
| 675 | class ThreadSafetyReporter : public clang::thread_safety::ThreadSafetyHandler { |
| 676 | Sema &S; |
| 677 | DiagList Warnings; |
| 678 | |
| 679 | // Helper functions |
| 680 | void warnLockMismatch(unsigned DiagID, Name LockName, SourceLocation Loc) { |
| 681 | PartialDiagnostic Warning = S.PDiag(DiagID) << LockName; |
| 682 | Warnings.push_back(DelayedDiag(Loc, Warning)); |
| 683 | } |
| 684 | |
| 685 | public: |
| 686 | ThreadSafetyReporter(Sema &S) : S(S) {} |
| 687 | |
| 688 | /// \brief Emit all buffered diagnostics in order of sourcelocation. |
| 689 | /// We need to output diagnostics produced while iterating through |
| 690 | /// the lockset in deterministic order, so this function orders diagnostics |
| 691 | /// and outputs them. |
| 692 | void emitDiagnostics() { |
| 693 | SortDiagBySourceLocation SortDiagBySL(S); |
| 694 | sort(Warnings.begin(), Warnings.end(), SortDiagBySL); |
| 695 | for (DiagList::iterator I = Warnings.begin(), E = Warnings.end(); |
| 696 | I != E; ++I) |
| 697 | S.Diag(I->first, I->second); |
| 698 | } |
| 699 | |
| 700 | void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) { |
| 701 | warnLockMismatch(diag::warn_unlock_but_no_lock, LockName, Loc); |
| 702 | } |
| 703 | |
| 704 | void handleDoubleLock(Name LockName, SourceLocation Loc) { |
| 705 | warnLockMismatch(diag::warn_double_lock, LockName, Loc); |
| 706 | } |
| 707 | |
| 708 | void handleMutexHeldEndOfScope(Name LockName, SourceLocation Loc){ |
| 709 | warnLockMismatch(diag::warn_lock_at_end_of_scope, LockName, Loc); |
| 710 | } |
| 711 | |
| 712 | void handleNoLockLoopEntry(Name LockName, SourceLocation Loc) { |
| 713 | warnLockMismatch(diag::warn_expecting_lock_held_on_loop, LockName, Loc); |
| 714 | } |
| 715 | |
| 716 | void handleNoUnlock(Name LockName, llvm::StringRef FunName, |
| 717 | SourceLocation Loc) { |
| 718 | PartialDiagnostic Warning = |
| 719 | S.PDiag(diag::warn_no_unlock) << LockName << FunName; |
| 720 | Warnings.push_back(DelayedDiag(Loc, Warning)); |
| 721 | } |
| 722 | |
| 723 | void handleExclusiveAndShared(Name LockName, SourceLocation Loc1, |
| 724 | SourceLocation Loc2) { |
| 725 | PartialDiagnostic Warning = |
| 726 | S.PDiag(diag::warn_lock_exclusive_and_shared) << LockName; |
| 727 | PartialDiagnostic Note = |
| 728 | S.PDiag(diag::note_lock_exclusive_and_shared) << LockName; |
| 729 | Warnings.push_back(DelayedDiag(Loc1, Warning)); |
| 730 | Warnings.push_back(DelayedDiag(Loc2, Note)); |
| 731 | } |
| 732 | |
| 733 | void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, |
| 734 | AccessKind AK, SourceLocation Loc) { |
Caitlin Sadowski | a49d1d8 | 2011-09-09 16:07:55 +0000 | [diff] [blame^] | 735 | // FIXME: It would be nice if this case printed without single quotes around |
| 736 | // the phrase 'any mutex' |
| 737 | handleMutexNotHeld(D, POK, "any mutex", getLockKindFromAccessKind(AK), Loc); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | void handleMutexNotHeld(const NamedDecl *D, ProtectedOperationKind POK, |
| 741 | Name LockName, LockKind LK, SourceLocation Loc) { |
| 742 | unsigned DiagID; |
| 743 | switch (POK) { |
| 744 | case POK_VarAccess: |
| 745 | DiagID = diag::warn_variable_requires_lock; |
| 746 | break; |
| 747 | case POK_VarDereference: |
| 748 | DiagID = diag::warn_var_deref_requires_lock; |
| 749 | break; |
| 750 | case POK_FunctionCall: |
| 751 | DiagID = diag::warn_fun_requires_lock; |
| 752 | break; |
| 753 | } |
| 754 | PartialDiagnostic Warning = S.PDiag(DiagID) |
| 755 | << D->getName().str() << LockName << LK; |
| 756 | Warnings.push_back(DelayedDiag(Loc, Warning)); |
| 757 | } |
| 758 | |
| 759 | void handleFunExcludesLock(Name FunName, Name LockName, SourceLocation Loc) { |
| 760 | PartialDiagnostic Warning = |
| 761 | S.PDiag(diag::warn_fun_excludes_mutex) << FunName << LockName; |
| 762 | Warnings.push_back(DelayedDiag(Loc, Warning)); |
| 763 | } |
| 764 | }; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | using namespace thread_safety; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 769 | |
| 770 | namespace { |
| 771 | /// \brief Implements a set of CFGBlocks using a BitVector. |
| 772 | /// |
| 773 | /// This class contains a minimal interface, primarily dictated by the SetType |
| 774 | /// template parameter of the llvm::po_iterator template, as used with external |
| 775 | /// storage. We also use this set to keep track of which CFGBlocks we visit |
| 776 | /// during the analysis. |
| 777 | class CFGBlockSet { |
| 778 | llvm::BitVector VisitedBlockIDs; |
| 779 | |
| 780 | public: |
| 781 | // po_iterator requires this iterator, but the only interface needed is the |
| 782 | // value_type typedef. |
| 783 | struct iterator { |
| 784 | typedef const CFGBlock *value_type; |
| 785 | }; |
| 786 | |
| 787 | CFGBlockSet() {} |
| 788 | CFGBlockSet(const CFG *G) : VisitedBlockIDs(G->getNumBlockIDs(), false) {} |
| 789 | |
| 790 | /// \brief Set the bit associated with a particular CFGBlock. |
| 791 | /// This is the important method for the SetType template parameter. |
| 792 | bool insert(const CFGBlock *Block) { |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 793 | // Note that insert() is called by po_iterator, which doesn't check to make |
| 794 | // sure that Block is non-null. Moreover, the CFGBlock iterator will |
| 795 | // occasionally hand out null pointers for pruned edges, so we catch those |
| 796 | // here. |
| 797 | if (Block == 0) |
| 798 | return false; // if an edge is trivially false. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 799 | if (VisitedBlockIDs.test(Block->getBlockID())) |
| 800 | return false; |
| 801 | VisitedBlockIDs.set(Block->getBlockID()); |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | /// \brief Check if the bit for a CFGBlock has been already set. |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 806 | /// This method is for tracking visited blocks in the main threadsafety loop. |
| 807 | /// Block must not be null. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 808 | bool alreadySet(const CFGBlock *Block) { |
| 809 | return VisitedBlockIDs.test(Block->getBlockID()); |
| 810 | } |
| 811 | }; |
| 812 | |
| 813 | /// \brief We create a helper class which we use to iterate through CFGBlocks in |
| 814 | /// the topological order. |
| 815 | class TopologicallySortedCFG { |
| 816 | typedef llvm::po_iterator<const CFG*, CFGBlockSet, true> po_iterator; |
| 817 | |
| 818 | std::vector<const CFGBlock*> Blocks; |
| 819 | |
| 820 | public: |
| 821 | typedef std::vector<const CFGBlock*>::reverse_iterator iterator; |
| 822 | |
| 823 | TopologicallySortedCFG(const CFG *CFGraph) { |
| 824 | Blocks.reserve(CFGraph->getNumBlockIDs()); |
| 825 | CFGBlockSet BSet(CFGraph); |
| 826 | |
| 827 | for (po_iterator I = po_iterator::begin(CFGraph, BSet), |
| 828 | E = po_iterator::end(CFGraph, BSet); I != E; ++I) { |
| 829 | Blocks.push_back(*I); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | iterator begin() { |
| 834 | return Blocks.rbegin(); |
| 835 | } |
| 836 | |
| 837 | iterator end() { |
| 838 | return Blocks.rend(); |
| 839 | } |
| 840 | }; |
| 841 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 842 | /// \brief A MutexID object uniquely identifies a particular mutex, and |
Caitlin Sadowski | 940b97f | 2011-08-24 18:46:20 +0000 | [diff] [blame] | 843 | /// is built from an Expr* (i.e. calling a lock function). |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 844 | /// |
| 845 | /// Thread-safety analysis works by comparing lock expressions. Within the |
| 846 | /// body of a function, an expression such as "x->foo->bar.mu" will resolve to |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 847 | /// a particular mutex object at run-time. Subsequent occurrences of the same |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 848 | /// expression (where "same" means syntactic equality) will refer to the same |
| 849 | /// run-time object if three conditions hold: |
| 850 | /// (1) Local variables in the expression, such as "x" have not changed. |
| 851 | /// (2) Values on the heap that affect the expression have not changed. |
| 852 | /// (3) The expression involves only pure function calls. |
| 853 | /// The current implementation assumes, but does not verify, that multiple uses |
| 854 | /// of the same lock expression satisfies these criteria. |
| 855 | /// |
| 856 | /// Clang introduces an additional wrinkle, which is that it is difficult to |
| 857 | /// derive canonical expressions, or compare expressions directly for equality. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 858 | /// Thus, we identify a mutex not by an Expr, but by the set of named |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 859 | /// declarations that are referenced by the Expr. In other words, |
| 860 | /// x->foo->bar.mu will be a four element vector with the Decls for |
| 861 | /// mu, bar, and foo, and x. The vector will uniquely identify the expression |
| 862 | /// for all practical purposes. |
| 863 | /// |
| 864 | /// Note we will need to perform substitution on "this" and function parameter |
| 865 | /// names when constructing a lock expression. |
| 866 | /// |
| 867 | /// For example: |
| 868 | /// class C { Mutex Mu; void lock() EXCLUSIVE_LOCK_FUNCTION(this->Mu); }; |
| 869 | /// void myFunc(C *X) { ... X->lock() ... } |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 870 | /// The original expression for the mutex acquired by myFunc is "this->Mu", but |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 871 | /// "X" is substituted for "this" so we get X->Mu(); |
| 872 | /// |
| 873 | /// For another example: |
| 874 | /// foo(MyList *L) EXCLUSIVE_LOCKS_REQUIRED(L->Mu) { ... } |
| 875 | /// MyList *MyL; |
| 876 | /// foo(MyL); // requires lock MyL->Mu to be held |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 877 | class MutexID { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 878 | SmallVector<NamedDecl*, 2> DeclSeq; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 879 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 880 | /// Build a Decl sequence representing the lock from the given expression. |
| 881 | /// Recursive function that bottoms out when the final DeclRefExpr is reached. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 882 | void buildMutexID(Expr *Exp) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 883 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) { |
| 884 | NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 885 | DeclSeq.push_back(ND); |
| 886 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 887 | NamedDecl *ND = ME->getMemberDecl(); |
| 888 | DeclSeq.push_back(ND); |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 889 | buildMutexID(ME->getBase()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 890 | } else if (isa<CXXThisExpr>(Exp)) { |
| 891 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 892 | } else { |
| 893 | // FIXME: add diagnostic |
| 894 | llvm::report_fatal_error("Expected lock expression!"); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | public: |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 899 | MutexID(Expr *LExpr) { |
| 900 | buildMutexID(LExpr); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 901 | assert(!DeclSeq.empty()); |
| 902 | } |
| 903 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 904 | bool operator==(const MutexID &other) const { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 905 | return DeclSeq == other.DeclSeq; |
| 906 | } |
| 907 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 908 | bool operator!=(const MutexID &other) const { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 909 | return !(*this == other); |
| 910 | } |
| 911 | |
| 912 | // SmallVector overloads Operator< to do lexicographic ordering. Note that |
| 913 | // we use pointer equality (and <) to compare NamedDecls. This means the order |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 914 | // of MutexIDs in a lockset is nondeterministic. In order to output |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 915 | // diagnostics in a deterministic ordering, we must order all diagnostics to |
| 916 | // output by SourceLocation when iterating through this lockset. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 917 | bool operator<(const MutexID &other) const { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 918 | return DeclSeq < other.DeclSeq; |
| 919 | } |
| 920 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 921 | /// \brief Returns the name of the first Decl in the list for a given MutexID; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 922 | /// e.g. the lock expression foo.bar() has name "bar". |
| 923 | /// The caret will point unambiguously to the lock expression, so using this |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 924 | /// name in diagnostics is a way to get simple, and consistent, mutex names. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 925 | /// We do not want to output the entire expression text for security reasons. |
| 926 | StringRef getName() const { |
| 927 | return DeclSeq.front()->getName(); |
| 928 | } |
| 929 | |
| 930 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 931 | for (SmallVectorImpl<NamedDecl*>::const_iterator I = DeclSeq.begin(), |
| 932 | E = DeclSeq.end(); I != E; ++I) { |
| 933 | ID.AddPointer(*I); |
| 934 | } |
| 935 | } |
| 936 | }; |
| 937 | |
| 938 | /// \brief This is a helper class that stores info about the most recent |
| 939 | /// accquire of a Lock. |
| 940 | /// |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 941 | /// The main body of the analysis maps MutexIDs to LockDatas. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 942 | struct LockData { |
| 943 | SourceLocation AcquireLoc; |
| 944 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 945 | /// \brief LKind stores whether a lock is held shared or exclusively. |
| 946 | /// Note that this analysis does not currently support either re-entrant |
| 947 | /// locking or lock "upgrading" and "downgrading" between exclusive and |
| 948 | /// shared. |
| 949 | /// |
| 950 | /// FIXME: add support for re-entrant locking and lock up/downgrading |
| 951 | LockKind LKind; |
| 952 | |
| 953 | LockData(SourceLocation AcquireLoc, LockKind LKind) |
| 954 | : AcquireLoc(AcquireLoc), LKind(LKind) {} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 955 | |
| 956 | bool operator==(const LockData &other) const { |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 957 | return AcquireLoc == other.AcquireLoc && LKind == other.LKind; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | bool operator!=(const LockData &other) const { |
| 961 | return !(*this == other); |
| 962 | } |
| 963 | |
| 964 | void Profile(llvm::FoldingSetNodeID &ID) const { |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 965 | ID.AddInteger(AcquireLoc.getRawEncoding()); |
| 966 | ID.AddInteger(LKind); |
| 967 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 968 | }; |
| 969 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 970 | /// A Lockset maps each MutexID (defined above) to information about how it has |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 971 | /// been locked. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 972 | typedef llvm::ImmutableMap<MutexID, LockData> Lockset; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 973 | |
| 974 | /// \brief We use this class to visit different types of expressions in |
| 975 | /// CFGBlocks, and build up the lockset. |
| 976 | /// An expression may cause us to add or remove locks from the lockset, or else |
| 977 | /// output error messages related to missing locks. |
| 978 | /// FIXME: In future, we may be able to not inherit from a visitor. |
| 979 | class BuildLockset : public StmtVisitor<BuildLockset> { |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 980 | ThreadSafetyHandler &Handler; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 981 | Lockset LSet; |
| 982 | Lockset::Factory &LocksetFactory; |
| 983 | |
| 984 | // Helper functions |
Caitlin Sadowski | 940b97f | 2011-08-24 18:46:20 +0000 | [diff] [blame] | 985 | void removeLock(SourceLocation UnlockLoc, Expr *LockExp); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 986 | void addLock(SourceLocation LockLoc, Expr *LockExp, LockKind LK); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 987 | const ValueDecl *getValueDecl(Expr *Exp); |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 988 | void warnIfMutexNotHeld (const NamedDecl *D, Expr *Exp, AccessKind AK, |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 989 | Expr *MutexExp, ProtectedOperationKind POK); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 990 | void checkAccess(Expr *Exp, AccessKind AK); |
| 991 | void checkDereference(Expr *Exp, AccessKind AK); |
| 992 | |
| 993 | template <class AttrType> |
| 994 | void addLocksToSet(LockKind LK, Attr *Attr, CXXMemberCallExpr *Exp); |
| 995 | |
| 996 | /// \brief Returns true if the lockset contains a lock, regardless of whether |
| 997 | /// the lock is held exclusively or shared. |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 998 | bool locksetContains(MutexID Lock) const { |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 999 | return LSet.lookup(Lock); |
| 1000 | } |
| 1001 | |
| 1002 | /// \brief Returns true if the lockset contains a lock with the passed in |
| 1003 | /// locktype. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1004 | bool locksetContains(MutexID Lock, LockKind KindRequested) const { |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1005 | const LockData *LockHeld = LSet.lookup(Lock); |
| 1006 | return (LockHeld && KindRequested == LockHeld->LKind); |
| 1007 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1008 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1009 | /// \brief Returns true if the lockset contains a lock with at least the |
| 1010 | /// passed in locktype. So for example, if we pass in LK_Shared, this function |
| 1011 | /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in |
| 1012 | /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive. |
| 1013 | bool locksetContainsAtLeast(MutexID Lock, LockKind KindRequested) const { |
| 1014 | switch (KindRequested) { |
| 1015 | case LK_Shared: |
| 1016 | return locksetContains(Lock); |
| 1017 | case LK_Exclusive: |
| 1018 | return locksetContains(Lock, KindRequested); |
| 1019 | } |
| 1020 | } |
| 1021 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1022 | public: |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1023 | BuildLockset(ThreadSafetyHandler &Handler, Lockset LS, Lockset::Factory &F) |
| 1024 | : StmtVisitor<BuildLockset>(), Handler(Handler), LSet(LS), |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1025 | LocksetFactory(F) {} |
| 1026 | |
| 1027 | Lockset getLockset() { |
| 1028 | return LSet; |
| 1029 | } |
| 1030 | |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1031 | void VisitUnaryOperator(UnaryOperator *UO); |
| 1032 | void VisitBinaryOperator(BinaryOperator *BO); |
| 1033 | void VisitCastExpr(CastExpr *CE); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1034 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp); |
| 1035 | }; |
| 1036 | |
| 1037 | /// \brief Add a new lock to the lockset, warning if the lock is already there. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1038 | /// \param LockLoc The source location of the acquire |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1039 | /// \param LockExp The lock expression corresponding to the lock to be added |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1040 | void BuildLockset::addLock(SourceLocation LockLoc, Expr *LockExp, |
| 1041 | LockKind LK) { |
| 1042 | // FIXME: deal with acquired before/after annotations |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1043 | MutexID Mutex(LockExp); |
| 1044 | LockData NewLock(LockLoc, LK); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1045 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1046 | // FIXME: Don't always warn when we have support for reentrant locks. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1047 | if (locksetContains(Mutex)) |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1048 | Handler.handleDoubleLock(Mutex.getName(), LockLoc); |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1049 | LSet = LocksetFactory.add(LSet, Mutex, NewLock); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /// \brief Remove a lock from the lockset, warning if the lock is not there. |
| 1053 | /// \param LockExp The lock expression corresponding to the lock to be removed |
| 1054 | /// \param UnlockLoc The source location of the unlock (only used in error msg) |
Caitlin Sadowski | 940b97f | 2011-08-24 18:46:20 +0000 | [diff] [blame] | 1055 | void BuildLockset::removeLock(SourceLocation UnlockLoc, Expr *LockExp) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1056 | MutexID Mutex(LockExp); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1057 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1058 | Lockset NewLSet = LocksetFactory.remove(LSet, Mutex); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1059 | if(NewLSet == LSet) |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1060 | Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1061 | |
| 1062 | LSet = NewLSet; |
| 1063 | } |
| 1064 | |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1065 | /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs |
| 1066 | const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) { |
| 1067 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp)) |
| 1068 | return DR->getDecl(); |
| 1069 | |
| 1070 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) |
| 1071 | return ME->getMemberDecl(); |
| 1072 | |
| 1073 | return 0; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1076 | /// \brief Warn if the LSet does not contain a lock sufficient to protect access |
| 1077 | /// of at least the passed in AccessType. |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1078 | void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1079 | AccessKind AK, Expr *MutexExp, |
| 1080 | ProtectedOperationKind POK) { |
| 1081 | LockKind LK = getLockKindFromAccessKind(AK); |
| 1082 | MutexID Mutex(MutexExp); |
| 1083 | if (!locksetContainsAtLeast(Mutex, LK)) |
| 1084 | Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK, Exp->getExprLoc()); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1088 | /// \brief This method identifies variable dereferences and checks pt_guarded_by |
| 1089 | /// and pt_guarded_var annotations. Note that we only check these annotations |
| 1090 | /// at the time a pointer is dereferenced. |
| 1091 | /// FIXME: We need to check for other types of pointer dereferences |
| 1092 | /// (e.g. [], ->) and deal with them here. |
| 1093 | /// \param Exp An expression that has been read or written. |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1094 | void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) { |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1095 | UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp); |
| 1096 | if (!UO || UO->getOpcode() != clang::UO_Deref) |
| 1097 | return; |
| 1098 | Exp = UO->getSubExpr()->IgnoreParenCasts(); |
| 1099 | |
| 1100 | const ValueDecl *D = getValueDecl(Exp); |
| 1101 | if(!D || !D->hasAttrs()) |
| 1102 | return; |
| 1103 | |
| 1104 | if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty()) |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1105 | Handler.handleNoMutexHeld(D, POK_VarDereference, AK, Exp->getExprLoc()); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1106 | |
| 1107 | const AttrVec &ArgAttrs = D->getAttrs(); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1108 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 1109 | if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i])) |
| 1110 | warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /// \brief Checks guarded_by and guarded_var attributes. |
| 1114 | /// Whenever we identify an access (read or write) of a DeclRefExpr or |
| 1115 | /// MemberExpr, we need to check whether there are any guarded_by or |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1116 | /// guarded_var attributes, and make sure we hold the appropriate mutexes. |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1117 | void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) { |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1118 | const ValueDecl *D = getValueDecl(Exp); |
| 1119 | if(!D || !D->hasAttrs()) |
| 1120 | return; |
| 1121 | |
| 1122 | if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty()) |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1123 | Handler.handleNoMutexHeld(D, POK_VarAccess, AK, Exp->getExprLoc()); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1124 | |
| 1125 | const AttrVec &ArgAttrs = D->getAttrs(); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1126 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 1127 | if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i])) |
| 1128 | warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /// \brief For unary operations which read and write a variable, we need to |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1132 | /// check whether we hold any required mutexes. Reads are checked in |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1133 | /// VisitCastExpr. |
| 1134 | void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { |
| 1135 | switch (UO->getOpcode()) { |
| 1136 | case clang::UO_PostDec: |
| 1137 | case clang::UO_PostInc: |
| 1138 | case clang::UO_PreDec: |
| 1139 | case clang::UO_PreInc: { |
| 1140 | Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts(); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1141 | checkAccess(SubExp, AK_Written); |
| 1142 | checkDereference(SubExp, AK_Written); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | } |
| 1145 | default: |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /// For binary operations which assign to a variable (writes), we need to check |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1151 | /// whether we hold any required mutexes. |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1152 | /// FIXME: Deal with non-primitive types. |
| 1153 | void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { |
| 1154 | if (!BO->isAssignmentOp()) |
| 1155 | return; |
| 1156 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1157 | checkAccess(LHSExp, AK_Written); |
| 1158 | checkDereference(LHSExp, AK_Written); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /// Whenever we do an LValue to Rvalue cast, we are reading a variable and |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1162 | /// need to ensure we hold any required mutexes. |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1163 | /// FIXME: Deal with non-primitive types. |
| 1164 | void BuildLockset::VisitCastExpr(CastExpr *CE) { |
| 1165 | if (CE->getCastKind() != CK_LValueToRValue) |
| 1166 | return; |
| 1167 | Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts(); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1168 | checkAccess(SubExp, AK_Read); |
| 1169 | checkDereference(SubExp, AK_Read); |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1172 | /// \brief This function, parameterized by an attribute type, is used to add a |
| 1173 | /// set of locks specified as attribute arguments to the lockset. |
| 1174 | template <typename AttrType> |
| 1175 | void BuildLockset::addLocksToSet(LockKind LK, Attr *Attr, |
| 1176 | CXXMemberCallExpr *Exp) { |
| 1177 | typedef typename AttrType::args_iterator iterator_type; |
| 1178 | SourceLocation ExpLocation = Exp->getExprLoc(); |
| 1179 | Expr *Parent = Exp->getImplicitObjectArgument(); |
| 1180 | AttrType *SpecificAttr = cast<AttrType>(Attr); |
| 1181 | |
| 1182 | if (SpecificAttr->args_size() == 0) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1183 | // The mutex held is the "this" object. |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1184 | addLock(ExpLocation, Parent, LK); |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | for (iterator_type I = SpecificAttr->args_begin(), |
| 1189 | E = SpecificAttr->args_end(); I != E; ++I) |
| 1190 | addLock(ExpLocation, *I, LK); |
| 1191 | } |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1192 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1193 | /// \brief When visiting CXXMemberCallExprs we need to examine the attributes on |
| 1194 | /// the method that is being called and add, remove or check locks in the |
| 1195 | /// lockset accordingly. |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1196 | /// |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1197 | /// FIXME: For classes annotated with one of the guarded annotations, we need |
| 1198 | /// to treat const method calls as reads and non-const method calls as writes, |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1199 | /// and check that the appropriate locks are held. Non-const method calls with |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 1200 | /// the same signature as const method calls can be also treated as reads. |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1201 | /// |
| 1202 | /// FIXME: We need to also visit CallExprs to catch/check global functions. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1203 | void BuildLockset::VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp) { |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1204 | NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1205 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1206 | SourceLocation ExpLocation = Exp->getExprLoc(); |
| 1207 | Expr *Parent = Exp->getImplicitObjectArgument(); |
| 1208 | |
| 1209 | if(!D || !D->hasAttrs()) |
| 1210 | return; |
| 1211 | |
| 1212 | AttrVec &ArgAttrs = D->getAttrs(); |
| 1213 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 1214 | Attr *Attr = ArgAttrs[i]; |
| 1215 | switch (Attr->getKind()) { |
| 1216 | // When we encounter an exclusive lock function, we need to add the lock |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1217 | // to our lockset with kind exclusive. |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1218 | case attr::ExclusiveLockFunction: |
| 1219 | addLocksToSet<ExclusiveLockFunctionAttr>(LK_Exclusive, Attr, Exp); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1220 | break; |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1221 | |
| 1222 | // When we encounter a shared lock function, we need to add the lock |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1223 | // to our lockset with kind shared. |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1224 | case attr::SharedLockFunction: |
| 1225 | addLocksToSet<SharedLockFunctionAttr>(LK_Shared, Attr, Exp); |
| 1226 | break; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1227 | |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1228 | // When we encounter an unlock function, we need to remove unlocked |
| 1229 | // mutexes from the lockset, and flag a warning if they are not there. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1230 | case attr::UnlockFunction: { |
| 1231 | UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr); |
| 1232 | |
| 1233 | if (UFAttr->args_size() == 0) { // The lock held is the "this" object. |
Caitlin Sadowski | 940b97f | 2011-08-24 18:46:20 +0000 | [diff] [blame] | 1234 | removeLock(ExpLocation, Parent); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | } |
| 1237 | |
| 1238 | for (UnlockFunctionAttr::args_iterator I = UFAttr->args_begin(), |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1239 | E = UFAttr->args_end(); I != E; ++I) |
Caitlin Sadowski | 940b97f | 2011-08-24 18:46:20 +0000 | [diff] [blame] | 1240 | removeLock(ExpLocation, *I); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | } |
| 1243 | |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1244 | case attr::ExclusiveLocksRequired: { |
| 1245 | // FIXME: Also use this attribute to add required locks to the initial |
| 1246 | // lockset when processing a CFG for a function annotated with this |
| 1247 | // attribute. |
| 1248 | ExclusiveLocksRequiredAttr *ELRAttr = |
| 1249 | cast<ExclusiveLocksRequiredAttr>(Attr); |
| 1250 | |
| 1251 | for (ExclusiveLocksRequiredAttr::args_iterator |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1252 | I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I) |
| 1253 | warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall); |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | case attr::SharedLocksRequired: { |
| 1258 | // FIXME: Also use this attribute to add required locks to the initial |
| 1259 | // lockset when processing a CFG for a function annotated with this |
| 1260 | // attribute. |
| 1261 | SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr); |
| 1262 | |
| 1263 | for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(), |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1264 | E = SLRAttr->args_end(); I != E; ++I) |
| 1265 | warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall); |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | case attr::LocksExcluded: { |
| 1270 | LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr); |
| 1271 | for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(), |
| 1272 | E = LEAttr->args_end(); I != E; ++I) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1273 | MutexID Mutex(*I); |
| 1274 | if (locksetContains(Mutex)) |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1275 | Handler.handleFunExcludesLock(D->getName(), Mutex.getName(), |
| 1276 | ExpLocation); |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 1277 | } |
| 1278 | break; |
| 1279 | } |
| 1280 | |
| 1281 | case attr::LockReturned: |
| 1282 | // FIXME: Deal with this attribute. |
| 1283 | break; |
| 1284 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1285 | // Ignore other (non thread-safety) attributes |
| 1286 | default: |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1292 | } // end anonymous namespace |
| 1293 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1294 | /// \brief Flags a warning for each lock that is in LSet2 but not LSet1, or |
| 1295 | /// else mutexes that are held shared in one lockset and exclusive in the other. |
| 1296 | static Lockset warnIfNotInFirstSetOrNotSameKind(ThreadSafetyHandler &Handler, |
| 1297 | const Lockset LSet1, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1298 | const Lockset LSet2, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1299 | Lockset Intersection, |
| 1300 | Lockset::Factory &Fact) { |
| 1301 | for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1302 | const MutexID &LSet2Mutex = I.getKey(); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1303 | const LockData &LSet2LockData = I.getData(); |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1304 | if (const LockData *LD = LSet1.lookup(LSet2Mutex)) { |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1305 | if (LD->LKind != LSet2LockData.LKind) { |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1306 | Handler.handleExclusiveAndShared(LSet2Mutex.getName(), |
| 1307 | LSet2LockData.AcquireLoc, |
| 1308 | LD->AcquireLoc); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1309 | if (LD->LKind != LK_Exclusive) |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1310 | Intersection = Fact.add(Intersection, LSet2Mutex, LSet2LockData); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } else { |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1313 | Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(), |
| 1314 | LSet2LockData.AcquireLoc); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | return Intersection; |
| 1318 | } |
| 1319 | |
| 1320 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1321 | /// \brief Compute the intersection of two locksets and issue warnings for any |
| 1322 | /// locks in the symmetric difference. |
| 1323 | /// |
| 1324 | /// This function is used at a merge point in the CFG when comparing the lockset |
| 1325 | /// of each branch being merged. For example, given the following sequence: |
| 1326 | /// A; if () then B; else C; D; we need to check that the lockset after B and C |
| 1327 | /// are the same. In the event of a difference, we use the intersection of these |
| 1328 | /// two locksets at the start of D. |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1329 | static Lockset intersectAndWarn(ThreadSafetyHandler &Handler, |
| 1330 | const Lockset LSet1, const Lockset LSet2, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1331 | Lockset::Factory &Fact) { |
| 1332 | Lockset Intersection = LSet1; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1333 | Intersection = warnIfNotInFirstSetOrNotSameKind(Handler, LSet1, LSet2, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1334 | Intersection, Fact); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1335 | |
| 1336 | for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) { |
| 1337 | if (!LSet2.contains(I.getKey())) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1338 | const MutexID &Mutex = I.getKey(); |
| 1339 | const LockData &MissingLock = I.getData(); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1340 | Handler.handleMutexHeldEndOfScope(Mutex.getName(), |
| 1341 | MissingLock.AcquireLoc); |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1342 | Intersection = Fact.remove(Intersection, Mutex); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1345 | return Intersection; |
| 1346 | } |
| 1347 | |
| 1348 | /// \brief Returns the location of the first Stmt in a Block. |
| 1349 | static SourceLocation getFirstStmtLocation(CFGBlock *Block) { |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1350 | SourceLocation Loc; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1351 | for (CFGBlock::const_iterator BI = Block->begin(), BE = Block->end(); |
| 1352 | BI != BE; ++BI) { |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1353 | if (const CFGStmt *CfgStmt = dyn_cast<CFGStmt>(&(*BI))) { |
| 1354 | Loc = CfgStmt->getStmt()->getLocStart(); |
| 1355 | if (Loc.isValid()) return Loc; |
| 1356 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1357 | } |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1358 | if (Stmt *S = Block->getTerminator().getStmt()) { |
| 1359 | Loc = S->getLocStart(); |
| 1360 | if (Loc.isValid()) return Loc; |
| 1361 | } |
| 1362 | return Loc; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /// \brief Warn about different locksets along backedges of loops. |
| 1366 | /// This function is called when we encounter a back edge. At that point, |
| 1367 | /// we need to verify that the lockset before taking the backedge is the |
| 1368 | /// same as the lockset before entering the loop. |
| 1369 | /// |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1370 | /// \param LoopEntrySet Locks before starting the loop |
| 1371 | /// \param LoopReentrySet Locks in the last CFG block of the loop |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1372 | static void warnBackEdgeUnequalLocksets(ThreadSafetyHandler &Handler, |
| 1373 | const Lockset LoopReentrySet, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1374 | const Lockset LoopEntrySet, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1375 | SourceLocation FirstLocInLoop, |
| 1376 | Lockset::Factory &Fact) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1377 | assert(FirstLocInLoop.isValid()); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1378 | // Warn for locks held at the start of the loop, but not the end. |
| 1379 | for (Lockset::iterator I = LoopEntrySet.begin(), E = LoopEntrySet.end(); |
| 1380 | I != E; ++I) { |
| 1381 | if (!LoopReentrySet.contains(I.getKey())) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1382 | // We report this error at the location of the first statement in a loop |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1383 | Handler.handleNoLockLoopEntry(I.getKey().getName(), FirstLocInLoop); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // Warn for locks held at the end of the loop, but not at the start. |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1388 | warnIfNotInFirstSetOrNotSameKind(Handler, LoopEntrySet, LoopReentrySet, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1389 | LoopReentrySet, Fact); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1392 | |
| 1393 | namespace clang { namespace thread_safety { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1394 | /// \brief Check a function's CFG for thread-safety violations. |
| 1395 | /// |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1396 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1397 | /// at the end of each block, and issue warnings for thread safety violations. |
| 1398 | /// Each block in the CFG is traversed exactly once. |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1399 | void runThreadSafetyAnalysis(AnalysisContext &AC, |
| 1400 | ThreadSafetyHandler &Handler) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1401 | CFG *CFGraph = AC.getCFG(); |
| 1402 | if (!CFGraph) return; |
Caitlin Sadowski | af37061 | 2011-09-08 18:35:21 +0000 | [diff] [blame] | 1403 | const Decl *D = AC.getDecl(); |
| 1404 | if (D && D->getAttr<NoThreadSafetyAnalysisAttr>()) return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1405 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1406 | Lockset::Factory LocksetFactory; |
| 1407 | |
| 1408 | // FIXME: Swith to SmallVector? Otherwise improve performance impact? |
| 1409 | std::vector<Lockset> EntryLocksets(CFGraph->getNumBlockIDs(), |
| 1410 | LocksetFactory.getEmptyMap()); |
| 1411 | std::vector<Lockset> ExitLocksets(CFGraph->getNumBlockIDs(), |
| 1412 | LocksetFactory.getEmptyMap()); |
| 1413 | |
| 1414 | // We need to explore the CFG via a "topological" ordering. |
| 1415 | // That way, we will be guaranteed to have information about required |
| 1416 | // predecessor locksets when exploring a new block. |
| 1417 | TopologicallySortedCFG SortedGraph(CFGraph); |
| 1418 | CFGBlockSet VisitedBlocks(CFGraph); |
| 1419 | |
| 1420 | for (TopologicallySortedCFG::iterator I = SortedGraph.begin(), |
| 1421 | E = SortedGraph.end(); I!= E; ++I) { |
| 1422 | const CFGBlock *CurrBlock = *I; |
| 1423 | int CurrBlockID = CurrBlock->getBlockID(); |
| 1424 | |
| 1425 | VisitedBlocks.insert(CurrBlock); |
| 1426 | |
| 1427 | // Use the default initial lockset in case there are no predecessors. |
| 1428 | Lockset &Entryset = EntryLocksets[CurrBlockID]; |
| 1429 | Lockset &Exitset = ExitLocksets[CurrBlockID]; |
| 1430 | |
| 1431 | // Iterate through the predecessor blocks and warn if the lockset for all |
| 1432 | // predecessors is not the same. We take the entry lockset of the current |
| 1433 | // block to be the intersection of all previous locksets. |
| 1434 | // FIXME: By keeping the intersection, we may output more errors in future |
| 1435 | // for a lock which is not in the intersection, but was in the union. We |
| 1436 | // may want to also keep the union in future. As an example, let's say |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1437 | // the intersection contains Mutex L, and the union contains L and M. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1438 | // Later we unlock M. At this point, we would output an error because we |
| 1439 | // never locked M; although the real error is probably that we forgot to |
| 1440 | // lock M on all code paths. Conversely, let's say that later we lock M. |
| 1441 | // In this case, we should compare against the intersection instead of the |
| 1442 | // union because the real error is probably that we forgot to unlock M on |
| 1443 | // all code paths. |
| 1444 | bool LocksetInitialized = false; |
| 1445 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 1446 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 1447 | |
| 1448 | // if *PI -> CurrBlock is a back edge |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1449 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1450 | continue; |
| 1451 | |
| 1452 | int PrevBlockID = (*PI)->getBlockID(); |
| 1453 | if (!LocksetInitialized) { |
| 1454 | Entryset = ExitLocksets[PrevBlockID]; |
| 1455 | LocksetInitialized = true; |
| 1456 | } else { |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1457 | Entryset = intersectAndWarn(Handler, Entryset, |
| 1458 | ExitLocksets[PrevBlockID], LocksetFactory); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1462 | BuildLockset LocksetBuilder(Handler, Entryset, LocksetFactory); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1463 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 1464 | BE = CurrBlock->end(); BI != BE; ++BI) { |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1465 | if (const CFGStmt *CfgStmt = dyn_cast<CFGStmt>(&*BI)) |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 1466 | LocksetBuilder.Visit(const_cast<Stmt*>(CfgStmt->getStmt())); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1467 | } |
| 1468 | Exitset = LocksetBuilder.getLockset(); |
| 1469 | |
| 1470 | // For every back edge from CurrBlock (the end of the loop) to another block |
| 1471 | // (FirstLoopBlock) we need to check that the Lockset of Block is equal to |
| 1472 | // the one held at the beginning of FirstLoopBlock. We can look up the |
| 1473 | // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. |
| 1474 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 1475 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 1476 | |
| 1477 | // if CurrBlock -> *SI is *not* a back edge |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1478 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1479 | continue; |
| 1480 | |
| 1481 | CFGBlock *FirstLoopBlock = *SI; |
| 1482 | SourceLocation FirstLoopLocation = getFirstStmtLocation(FirstLoopBlock); |
| 1483 | |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1484 | assert(FirstLoopLocation.isValid()); |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1485 | |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1486 | // Fail gracefully in release code. |
| 1487 | if (!FirstLoopLocation.isValid()) |
| 1488 | continue; |
| 1489 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1490 | Lockset PreLoop = EntryLocksets[FirstLoopBlock->getBlockID()]; |
| 1491 | Lockset LoopEnd = ExitLocksets[CurrBlockID]; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1492 | warnBackEdgeUnequalLocksets(Handler, LoopEnd, PreLoop, FirstLoopLocation, |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 1493 | LocksetFactory); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | Lockset FinalLockset = ExitLocksets[CFGraph->getExit().getBlockID()]; |
| 1498 | if (!FinalLockset.isEmpty()) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1499 | for (Lockset::iterator I=FinalLockset.begin(), E=FinalLockset.end(); |
| 1500 | I != E; ++I) { |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 1501 | const MutexID &Mutex = I.getKey(); |
| 1502 | const LockData &MissingLock = I.getData(); |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 1503 | |
| 1504 | std::string FunName = "<unknown>"; |
| 1505 | if (const NamedDecl *ContextDecl = dyn_cast<NamedDecl>(AC.getDecl())) { |
| 1506 | FunName = ContextDecl->getDeclName().getAsString(); |
| 1507 | } |
| 1508 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1509 | Handler.handleNoUnlock(Mutex.getName(), FunName, MissingLock.AcquireLoc); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1510 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1514 | }} // end namespace clang::thread_safety |
| 1515 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1516 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1517 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1518 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 1519 | // warnings on a function, method, or block. |
| 1520 | //===----------------------------------------------------------------------===// |
| 1521 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1522 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 1523 | enableCheckFallThrough = 1; |
| 1524 | enableCheckUnreachable = 0; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1525 | enableThreadSafetyAnalysis = 0; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1528 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) |
| 1529 | : S(s), |
| 1530 | NumFunctionsAnalyzed(0), |
Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1531 | NumFunctionsWithBadCFGs(0), |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1532 | NumCFGBlocks(0), |
Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1533 | MaxCFGBlocksPerFunction(0), |
| 1534 | NumUninitAnalysisFunctions(0), |
| 1535 | NumUninitAnalysisVariables(0), |
| 1536 | MaxUninitAnalysisVariablesPerFunction(0), |
| 1537 | NumUninitAnalysisBlockVisits(0), |
| 1538 | MaxUninitAnalysisBlockVisitsPerFunction(0) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1539 | Diagnostic &D = S.getDiagnostics(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1540 | DefaultPolicy.enableCheckUnreachable = (unsigned) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1541 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != |
| 1542 | Diagnostic::Ignored); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1543 | DefaultPolicy.enableThreadSafetyAnalysis = (unsigned) |
| 1544 | (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) != |
| 1545 | Diagnostic::Ignored); |
| 1546 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1549 | static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1550 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1551 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 1552 | e = fscope->PossiblyUnreachableDiags.end(); |
| 1553 | i != e; ++i) { |
| 1554 | const sema::PossiblyUnreachableDiag &D = *i; |
| 1555 | S.Diag(D.Loc, D.PD); |
| 1556 | } |
| 1557 | } |
| 1558 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1559 | void clang::sema:: |
| 1560 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 1561 | sema::FunctionScopeInfo *fscope, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1562 | const Decl *D, const BlockExpr *blkExpr) { |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 1563 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1564 | // We avoid doing analysis-based warnings when there are errors for |
| 1565 | // two reasons: |
| 1566 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 1567 | // don't bother trying. |
| 1568 | // (2) The code already has problems; running the analysis just takes more |
| 1569 | // time. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1570 | Diagnostic &Diags = S.getDiagnostics(); |
| 1571 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1572 | // Do not do any analysis for declarations in system headers if we are |
| 1573 | // going to just ignore them. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1574 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1575 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 1576 | return; |
| 1577 | |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 1578 | // For code in dependent contexts, we'll do this at instantiation time. |
| 1579 | if (cast<DeclContext>(D)->isDependentContext()) |
| 1580 | return; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1581 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1582 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) { |
| 1583 | // Flush out any possibly unreachable diagnostics. |
| 1584 | flushDiagnostics(S, fscope); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1588 | const Stmt *Body = D->getBody(); |
| 1589 | assert(Body); |
| 1590 | |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1591 | AnalysisContext AC(D, 0); |
| 1592 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1593 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
| 1594 | // explosion for destrutors that can result and the compile time hit. |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1595 | AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true; |
| 1596 | AC.getCFGBuildOptions().AddEHEdges = false; |
| 1597 | AC.getCFGBuildOptions().AddInitializers = true; |
| 1598 | AC.getCFGBuildOptions().AddImplicitDtors = true; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Force that certain expressions appear as CFGElements in the CFG. This |
| 1601 | // is used to speed up various analyses. |
| 1602 | // FIXME: This isn't the right factoring. This is here for initial |
| 1603 | // prototyping, but we need a way for analyses to say what expressions they |
| 1604 | // expect to always be CFGElements and then fill in the BuildOptions |
| 1605 | // appropriately. This is essentially a layering violation. |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1606 | if (P.enableCheckUnreachable) { |
| 1607 | // Unreachable code analysis requires a linearized CFG. |
| 1608 | AC.getCFGBuildOptions().setAllAlwaysAdd(); |
| 1609 | } |
| 1610 | else { |
| 1611 | AC.getCFGBuildOptions() |
| 1612 | .setAlwaysAdd(Stmt::BinaryOperatorClass) |
| 1613 | .setAlwaysAdd(Stmt::BlockExprClass) |
| 1614 | .setAlwaysAdd(Stmt::CStyleCastExprClass) |
| 1615 | .setAlwaysAdd(Stmt::DeclRefExprClass) |
| 1616 | .setAlwaysAdd(Stmt::ImplicitCastExprClass) |
| 1617 | .setAlwaysAdd(Stmt::UnaryOperatorClass); |
| 1618 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1619 | |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1620 | // Construct the analysis context with the specified CFG build options. |
| 1621 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1622 | // Emit delayed diagnostics. |
| 1623 | if (!fscope->PossiblyUnreachableDiags.empty()) { |
| 1624 | bool analyzed = false; |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1625 | |
| 1626 | // Register the expressions with the CFGBuilder. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1627 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1628 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 1629 | e = fscope->PossiblyUnreachableDiags.end(); |
| 1630 | i != e; ++i) { |
| 1631 | if (const Stmt *stmt = i->stmt) |
| 1632 | AC.registerForcedBlockExpression(stmt); |
| 1633 | } |
| 1634 | |
| 1635 | if (AC.getCFG()) { |
| 1636 | analyzed = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1637 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1638 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 1639 | e = fscope->PossiblyUnreachableDiags.end(); |
| 1640 | i != e; ++i) |
| 1641 | { |
| 1642 | const sema::PossiblyUnreachableDiag &D = *i; |
| 1643 | bool processed = false; |
| 1644 | if (const Stmt *stmt = i->stmt) { |
| 1645 | const CFGBlock *block = AC.getBlockForRegisteredExpression(stmt); |
| 1646 | assert(block); |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 1647 | if (CFGReverseBlockReachabilityAnalysis *cra = AC.getCFGReachablityAnalysis()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1648 | // Can this block be reached from the entrance? |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1649 | if (cra->isReachable(&AC.getCFG()->getEntry(), block)) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1650 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1651 | processed = true; |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1652 | } |
| 1653 | } |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1654 | if (!processed) { |
| 1655 | // Emit the warning anyway if we cannot map to a basic block. |
| 1656 | S.Diag(D.Loc, D.PD); |
| 1657 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1658 | } |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1659 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (!analyzed) |
| 1662 | flushDiagnostics(S, fscope); |
| 1663 | } |
| 1664 | |
| 1665 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1666 | // Warning: check missing 'return' |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1667 | if (P.enableCheckFallThrough) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1668 | const CheckFallThroughDiagnostics &CD = |
| 1669 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 1670 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1671 | CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | // Warning: check for unreachable code |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 1675 | if (P.enableCheckUnreachable) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1676 | CheckUnreachable(S, AC); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1677 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1678 | // Check for thread safety violations |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1679 | if (P.enableThreadSafetyAnalysis) { |
| 1680 | thread_safety::ThreadSafetyReporter Reporter(S); |
| 1681 | thread_safety::runThreadSafetyAnalysis(AC, Reporter); |
| 1682 | Reporter.emitDiagnostics(); |
| 1683 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1684 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 1685 | if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart()) |
Ted Kremenek | 76709bf | 2011-03-15 05:22:28 +0000 | [diff] [blame] | 1686 | != Diagnostic::Ignored || |
| 1687 | Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart()) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1688 | != Diagnostic::Ignored) { |
Ted Kremenek | c5e43c1 | 2011-03-17 05:29:57 +0000 | [diff] [blame] | 1689 | if (CFG *cfg = AC.getCFG()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 1690 | UninitValsDiagReporter reporter(S); |
Fariborz Jahanian | 57080fb | 2011-07-16 18:31:33 +0000 | [diff] [blame] | 1691 | UninitVariablesAnalysisStats stats; |
Benjamin Kramer | 12efd57 | 2011-07-16 20:13:06 +0000 | [diff] [blame] | 1692 | std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats)); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 1693 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1694 | reporter, stats); |
| 1695 | |
| 1696 | if (S.CollectStats && stats.NumVariablesAnalyzed > 0) { |
| 1697 | ++NumUninitAnalysisFunctions; |
| 1698 | NumUninitAnalysisVariables += stats.NumVariablesAnalyzed; |
| 1699 | NumUninitAnalysisBlockVisits += stats.NumBlockVisits; |
| 1700 | MaxUninitAnalysisVariablesPerFunction = |
| 1701 | std::max(MaxUninitAnalysisVariablesPerFunction, |
| 1702 | stats.NumVariablesAnalyzed); |
| 1703 | MaxUninitAnalysisBlockVisitsPerFunction = |
| 1704 | std::max(MaxUninitAnalysisBlockVisitsPerFunction, |
| 1705 | stats.NumBlockVisits); |
| 1706 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1709 | |
| 1710 | // Collect statistics about the CFG if it was built. |
| 1711 | if (S.CollectStats && AC.isCFGBuilt()) { |
| 1712 | ++NumFunctionsAnalyzed; |
| 1713 | if (CFG *cfg = AC.getCFG()) { |
| 1714 | // If we successfully built a CFG for this context, record some more |
| 1715 | // detail information about it. |
Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 1716 | NumCFGBlocks += cfg->getNumBlockIDs(); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1717 | MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction, |
Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 1718 | cfg->getNumBlockIDs()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1719 | } else { |
| 1720 | ++NumFunctionsWithBadCFGs; |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | void clang::sema::AnalysisBasedWarnings::PrintStats() const { |
| 1726 | llvm::errs() << "\n*** Analysis Based Warnings Stats:\n"; |
| 1727 | |
| 1728 | unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs; |
| 1729 | unsigned AvgCFGBlocksPerFunction = |
| 1730 | !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt; |
| 1731 | llvm::errs() << NumFunctionsAnalyzed << " functions analyzed (" |
| 1732 | << NumFunctionsWithBadCFGs << " w/o CFGs).\n" |
| 1733 | << " " << NumCFGBlocks << " CFG blocks built.\n" |
| 1734 | << " " << AvgCFGBlocksPerFunction |
| 1735 | << " average CFG blocks per function.\n" |
| 1736 | << " " << MaxCFGBlocksPerFunction |
| 1737 | << " max CFG blocks per function.\n"; |
| 1738 | |
| 1739 | unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 1740 | : NumUninitAnalysisVariables/NumUninitAnalysisFunctions; |
| 1741 | unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 1742 | : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions; |
| 1743 | llvm::errs() << NumUninitAnalysisFunctions |
| 1744 | << " functions analyzed for uninitialiazed variables\n" |
| 1745 | << " " << NumUninitAnalysisVariables << " variables analyzed.\n" |
| 1746 | << " " << AvgUninitVariablesPerFunction |
| 1747 | << " average variables per function.\n" |
| 1748 | << " " << MaxUninitAnalysisVariablesPerFunction |
| 1749 | << " max variables per function.\n" |
| 1750 | << " " << NumUninitAnalysisBlockVisits << " block visits.\n" |
| 1751 | << " " << AvgUninitBlockVisitsPerFunction |
| 1752 | << " average block visits per function.\n" |
| 1753 | << " " << MaxUninitAnalysisBlockVisitsPerFunction |
| 1754 | << " max block visits per function.\n"; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1755 | } |