| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace ento; |
| 24 | using namespace ast_matchers; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | const char *WarnAtNode = "OSObjCast"; |
| 29 | |
| 30 | class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> { |
| 31 | public: |
| 32 | void checkASTCodeBody(const Decl *D, |
| 33 | AnalysisManager &AM, |
| 34 | BugReporter &BR) const; |
| 35 | }; |
| 36 | |
| 37 | static void emitDiagnostics(const BoundNodes &Nodes, |
| 38 | BugReporter &BR, |
| 39 | AnalysisDeclContext *ADC, |
| 40 | const OSObjectCStyleCastChecker *Checker) { |
| 41 | const auto *CE = Nodes.getNodeAs<CastExpr>(WarnAtNode); |
| 42 | assert(CE); |
| 43 | |
| 44 | std::string Diagnostics; |
| 45 | llvm::raw_string_ostream OS(Diagnostics); |
| 46 | OS << "C-style cast of OSObject. Use OSDynamicCast instead."; |
| 47 | |
| 48 | BR.EmitBasicReport( |
| 49 | ADC->getDecl(), |
| 50 | Checker, |
| 51 | , |
| 52 | , |
| 53 | OS.str(), |
| 54 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC), |
| 55 | CE->getSourceRange()); |
| 56 | } |
| 57 | |
| 58 | static auto hasTypePointingTo(DeclarationMatcher DeclM) |
| 59 | -> decltype(hasType(pointerType())) { |
| 60 | return hasType(pointerType(pointee(hasDeclaration(DeclM)))); |
| 61 | } |
| 62 | |
| 63 | void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM, |
| 64 | BugReporter &BR) const { |
| 65 | |
| 66 | AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D); |
| 67 | |
| 68 | auto DynamicCastM = callExpr(callee(functionDecl(hasName("safeMetaCast")))); |
| 69 | |
| 70 | auto OSObjTypeM = hasTypePointingTo(cxxRecordDecl(isDerivedFrom("OSMetaClassBase"))); |
| 71 | auto OSObjSubclassM = hasTypePointingTo( |
| 72 | cxxRecordDecl(isDerivedFrom("OSObject"))); |
| 73 | |
| 74 | auto CastM = cStyleCastExpr( |
| 75 | allOf(hasSourceExpression(allOf(OSObjTypeM, unless(DynamicCastM))), |
| 76 | OSObjSubclassM)).bind(WarnAtNode); |
| 77 | |
| 78 | auto Matches = match(stmt(forEachDescendant(CastM)), *D->getBody(), AM.getASTContext()); |
| 79 | for (BoundNodes Match : Matches) |
| 80 | emitDiagnostics(Match, BR, ADC, this); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ento::registerOSObjectCStyleCast(CheckerManager &Mgr) { |
| 85 | Mgr.registerChecker<OSObjectCStyleCastChecker>(); |
| 86 | } |
| 87 | |
| 88 | bool ento::shouldRegisterOSObjectCStyleCast(const LangOptions &LO) { |
| 89 | return true; |
| 90 | } |
| 91 | |