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/Optional.h" 21 #include "llvm/ADT/StringRef.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.hasValue() && 44 SourceManager.getFileOffset(NextToken->getLocation()) < 45 AnnotationBegin) { 46 if (NextToken->isNot(tok::semi)) 47 return false; 48 49 NextToken = Lexer::findNextToken(NextToken->getEndLoc(), SourceManager, 50 LangOptions); 51 } 52 53 return true; 54 } 55 56 llvm::Expected<llvm::DenseMap<const Stmt *, std::string>> 57 test::buildStatementToAnnotationMapping(const FunctionDecl *Func, 58 llvm::Annotations AnnotatedCode) { 59 llvm::DenseMap<const Stmt *, std::string> Result; 60 61 auto StmtMatcher = 62 findAll(stmt(unless(anyOf(hasParent(expr()), hasParent(returnStmt())))) 63 .bind("stmt")); 64 65 // This map should stay sorted because the binding algorithm relies on the 66 // ordering of statement offsets 67 std::map<unsigned, const Stmt *> Stmts; 68 auto &Context = Func->getASTContext(); 69 auto &SourceManager = Context.getSourceManager(); 70 71 for (auto &Match : match(StmtMatcher, *Func->getBody(), Context)) { 72 const auto *S = Match.getNodeAs<Stmt>("stmt"); 73 unsigned Offset = SourceManager.getFileOffset(S->getEndLoc()); 74 Stmts[Offset] = S; 75 } 76 77 unsigned I = 0; 78 auto Annotations = AnnotatedCode.ranges(); 79 std::reverse(Annotations.begin(), Annotations.end()); 80 auto Code = AnnotatedCode.code(); 81 82 for (auto OffsetAndStmt = Stmts.rbegin(); OffsetAndStmt != Stmts.rend(); 83 OffsetAndStmt++) { 84 unsigned Offset = OffsetAndStmt->first; 85 const Stmt *Stmt = OffsetAndStmt->second; 86 87 if (I < Annotations.size() && Annotations[I].Begin >= Offset) { 88 auto Range = Annotations[I]; 89 90 if (!isAnnotationDirectlyAfterStatement(Stmt, Range.Begin, SourceManager, 91 Context.getLangOpts())) { 92 return llvm::createStringError( 93 std::make_error_code(std::errc::invalid_argument), 94 "Annotation is not placed after a statement: %s", 95 SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()) 96 .getLocWithOffset(Offset) 97 .printToString(SourceManager) 98 .data()); 99 } 100 101 Result[Stmt] = Code.slice(Range.Begin, Range.End).str(); 102 I++; 103 104 if (I < Annotations.size() && Annotations[I].Begin >= Offset) { 105 return llvm::createStringError( 106 std::make_error_code(std::errc::invalid_argument), 107 "Multiple annotations bound to the statement at the location: %s", 108 Stmt->getBeginLoc().printToString(SourceManager).data()); 109 } 110 } 111 } 112 113 if (I < Annotations.size()) { 114 return llvm::createStringError( 115 std::make_error_code(std::errc::invalid_argument), 116 "Not all annotations were bound to statements. Unbound annotation at: " 117 "%s", 118 SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()) 119 .getLocWithOffset(Annotations[I].Begin) 120 .printToString(SourceManager) 121 .data()); 122 } 123 124 return Result; 125 } 126 127 const ValueDecl *test::findValueDecl(ASTContext &ASTCtx, llvm::StringRef Name) { 128 auto TargetNodes = match(valueDecl(hasName(Name)).bind("v"), ASTCtx); 129 assert(TargetNodes.size() == 1 && "Name must be unique"); 130 auto *const Result = selectFirst<ValueDecl>("v", TargetNodes); 131 assert(Result != nullptr); 132 return Result; 133 } 134