1 #include "TestingSupport.h" 2 #include "clang/AST/ASTContext.h" 3 #include "clang/AST/Decl.h" 4 #include "clang/AST/Stmt.h" 5 #include "clang/ASTMatchers/ASTMatchFinder.h" 6 #include "clang/ASTMatchers/ASTMatchers.h" 7 #include "clang/Analysis/CFG.h" 8 #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" 9 #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" 10 #include "clang/Basic/LLVM.h" 11 #include "clang/Basic/LangOptions.h" 12 #include "clang/Basic/SourceManager.h" 13 #include "clang/Basic/TokenKinds.h" 14 #include "clang/Lex/Lexer.h" 15 #include "clang/Serialization/PCHContainerOperations.h" 16 #include "clang/Tooling/ArgumentsAdjusters.h" 17 #include "clang/Tooling/Tooling.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/StringSet.h" 22 #include "llvm/Support/Error.h" 23 #include "llvm/Testing/Support/Annotations.h" 24 #include <cassert> 25 #include <functional> 26 #include <memory> 27 #include <string> 28 #include <system_error> 29 #include <utility> 30 #include <vector> 31 32 using namespace clang; 33 using namespace dataflow; 34 using namespace ast_matchers; 35 36 static bool 37 isAnnotationDirectlyAfterStatement(const Stmt *Stmt, unsigned AnnotationBegin, 38 const SourceManager &SourceManager, 39 const LangOptions &LangOptions) { 40 auto NextToken = 41 Lexer::findNextToken(Stmt->getEndLoc(), SourceManager, LangOptions); 42 43 while (NextToken && SourceManager.getFileOffset(NextToken->getLocation()) < 44 AnnotationBegin) { 45 if (NextToken->isNot(tok::semi)) 46 return false; 47 48 NextToken = Lexer::findNextToken(NextToken->getEndLoc(), SourceManager, 49 LangOptions); 50 } 51 52 return true; 53 } 54 55 llvm::DenseMap<unsigned, std::string> 56 test::buildLineToAnnotationMapping(SourceManager &SM, 57 llvm::Annotations AnnotatedCode) { 58 llvm::DenseMap<unsigned, std::string> LineNumberToContent; 59 auto Code = AnnotatedCode.code(); 60 auto Annotations = AnnotatedCode.ranges(); 61 for (auto &AnnotationRange : Annotations) { 62 auto LineNumber = 63 SM.getPresumedLineNumber(SM.getLocForStartOfFile(SM.getMainFileID()) 64 .getLocWithOffset(AnnotationRange.Begin)); 65 auto Content = Code.slice(AnnotationRange.Begin, AnnotationRange.End).str(); 66 LineNumberToContent[LineNumber] = Content; 67 } 68 return LineNumberToContent; 69 } 70 71 llvm::Expected<llvm::DenseMap<const Stmt *, std::string>> 72 test::buildStatementToAnnotationMapping(const FunctionDecl *Func, 73 llvm::Annotations AnnotatedCode) { 74 llvm::DenseMap<const Stmt *, std::string> Result; 75 llvm::StringSet<> ExistingAnnotations; 76 77 auto StmtMatcher = 78 findAll(stmt(unless(anyOf(hasParent(expr()), hasParent(returnStmt())))) 79 .bind("stmt")); 80 81 // This map should stay sorted because the binding algorithm relies on the 82 // ordering of statement offsets 83 std::map<unsigned, const Stmt *> Stmts; 84 auto &Context = Func->getASTContext(); 85 auto &SourceManager = Context.getSourceManager(); 86 87 for (auto &Match : match(StmtMatcher, *Func->getBody(), Context)) { 88 const auto *S = Match.getNodeAs<Stmt>("stmt"); 89 unsigned Offset = SourceManager.getFileOffset(S->getEndLoc()); 90 Stmts[Offset] = S; 91 } 92 93 unsigned I = 0; 94 auto Annotations = AnnotatedCode.ranges(); 95 std::reverse(Annotations.begin(), Annotations.end()); 96 auto Code = AnnotatedCode.code(); 97 98 for (auto OffsetAndStmt = Stmts.rbegin(); OffsetAndStmt != Stmts.rend(); 99 OffsetAndStmt++) { 100 unsigned Offset = OffsetAndStmt->first; 101 const Stmt *Stmt = OffsetAndStmt->second; 102 103 if (I < Annotations.size() && Annotations[I].Begin >= Offset) { 104 auto Range = Annotations[I]; 105 106 if (!isAnnotationDirectlyAfterStatement(Stmt, Range.Begin, SourceManager, 107 Context.getLangOpts())) { 108 return llvm::createStringError( 109 std::make_error_code(std::errc::invalid_argument), 110 "Annotation is not placed after a statement: %s", 111 SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()) 112 .getLocWithOffset(Offset) 113 .printToString(SourceManager) 114 .data()); 115 } 116 117 auto Annotation = Code.slice(Range.Begin, Range.End).str(); 118 if (!ExistingAnnotations.insert(Annotation).second) { 119 return llvm::createStringError( 120 std::make_error_code(std::errc::invalid_argument), 121 "Repeated use of annotation: %s", Annotation.data()); 122 } 123 Result[Stmt] = std::move(Annotation); 124 125 I++; 126 127 if (I < Annotations.size() && Annotations[I].Begin >= Offset) { 128 return llvm::createStringError( 129 std::make_error_code(std::errc::invalid_argument), 130 "Multiple annotations bound to the statement at the location: %s", 131 Stmt->getBeginLoc().printToString(SourceManager).data()); 132 } 133 } 134 } 135 136 if (I < Annotations.size()) { 137 return llvm::createStringError( 138 std::make_error_code(std::errc::invalid_argument), 139 "Not all annotations were bound to statements. Unbound annotation at: " 140 "%s", 141 SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()) 142 .getLocWithOffset(Annotations[I].Begin) 143 .printToString(SourceManager) 144 .data()); 145 } 146 147 return Result; 148 } 149 150 const ValueDecl *test::findValueDecl(ASTContext &ASTCtx, llvm::StringRef Name) { 151 auto TargetNodes = match(valueDecl(hasName(Name)).bind("v"), ASTCtx); 152 assert(TargetNodes.size() == 1 && "Name must be unique"); 153 auto *const Result = selectFirst<ValueDecl>("v", TargetNodes); 154 assert(Result != nullptr); 155 return Result; 156 } 157