|
| 1 | +//===- ASTSrcLocProcessor.cpp --------------------------------*- 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 | +#include "ASTSrcLocProcessor.h" |
| 11 | + |
| 12 | +#include "clang/Frontend/CompilerInstance.h" |
| 13 | +#include "llvm/Support/JSON.h" |
| 14 | + |
| 15 | +using namespace clang::tooling; |
| 16 | +using namespace llvm; |
| 17 | +using namespace clang::ast_matchers; |
| 18 | + |
| 19 | +ASTSrcLocProcessor::ASTSrcLocProcessor(StringRef JsonPath) |
| 20 | + : JsonPath(JsonPath) { |
| 21 | + |
| 22 | + MatchFinder::MatchFinderOptions FinderOptions; |
| 23 | + |
| 24 | + Finder = std::make_unique<MatchFinder>(std::move(FinderOptions)); |
| 25 | + Finder->addMatcher( |
| 26 | + cxxRecordDecl( |
| 27 | + isDefinition(), |
| 28 | + isSameOrDerivedFrom( |
| 29 | + // TODO: Extend this with other clades |
| 30 | + namedDecl(hasName("clang::Stmt")).bind("nodeClade")), |
| 31 | + optionally(isDerivedFrom(cxxRecordDecl().bind("derivedFrom")))) |
| 32 | + .bind("className"), |
| 33 | + this); |
| 34 | +} |
| 35 | + |
| 36 | +std::unique_ptr<clang::ASTConsumer> |
| 37 | +ASTSrcLocProcessor::createASTConsumer(clang::CompilerInstance &Compiler, |
| 38 | + StringRef File) { |
| 39 | + return Finder->newASTConsumer(); |
| 40 | +} |
| 41 | + |
| 42 | +llvm::json::Object toJSON(llvm::StringMap<std::vector<StringRef>> const &Obj) { |
| 43 | + using llvm::json::toJSON; |
| 44 | + |
| 45 | + llvm::json::Object JsonObj; |
| 46 | + for (const auto &Item : Obj) { |
| 47 | + JsonObj[Item.first()] = Item.second; |
| 48 | + } |
| 49 | + return JsonObj; |
| 50 | +} |
| 51 | + |
| 52 | +llvm::json::Object toJSON(llvm::StringMap<StringRef> const &Obj) { |
| 53 | + using llvm::json::toJSON; |
| 54 | + |
| 55 | + llvm::json::Object JsonObj; |
| 56 | + for (const auto &Item : Obj) { |
| 57 | + JsonObj[Item.first()] = Item.second; |
| 58 | + } |
| 59 | + return JsonObj; |
| 60 | +} |
| 61 | + |
| 62 | +llvm::json::Object toJSON(ClassData const &Obj) { |
| 63 | + llvm::json::Object JsonObj; |
| 64 | + |
| 65 | + if (!Obj.ASTClassLocations.empty()) |
| 66 | + JsonObj["sourceLocations"] = Obj.ASTClassLocations; |
| 67 | + if (!Obj.ASTClassRanges.empty()) |
| 68 | + JsonObj["sourceRanges"] = Obj.ASTClassRanges; |
| 69 | + return JsonObj; |
| 70 | +} |
| 71 | + |
| 72 | +llvm::json::Object toJSON(llvm::StringMap<ClassData> const &Obj) { |
| 73 | + using llvm::json::toJSON; |
| 74 | + |
| 75 | + llvm::json::Object JsonObj; |
| 76 | + for (const auto &Item : Obj) { |
| 77 | + if (!Item.second.isEmpty()) |
| 78 | + JsonObj[Item.first()] = ::toJSON(Item.second); |
| 79 | + } |
| 80 | + return JsonObj; |
| 81 | +} |
| 82 | + |
| 83 | +void WriteJSON(std::string JsonPath, |
| 84 | + llvm::StringMap<StringRef> const &ClassInheritance, |
| 85 | + llvm::StringMap<std::vector<StringRef>> const &ClassesInClade, |
| 86 | + llvm::StringMap<ClassData> const &ClassEntries) { |
| 87 | + llvm::json::Object JsonObj; |
| 88 | + |
| 89 | + using llvm::json::toJSON; |
| 90 | + |
| 91 | + JsonObj["classInheritance"] = ::toJSON(ClassInheritance); |
| 92 | + JsonObj["classesInClade"] = ::toJSON(ClassesInClade); |
| 93 | + JsonObj["classEntries"] = ::toJSON(ClassEntries); |
| 94 | + |
| 95 | + std::error_code EC; |
| 96 | + llvm::raw_fd_ostream JsonOut(JsonPath, EC, llvm::sys::fs::F_Text); |
| 97 | + if (EC) |
| 98 | + return; |
| 99 | + |
| 100 | + llvm::json::Value JsonVal(std::move(JsonObj)); |
| 101 | + JsonOut << formatv("{0:2}", JsonVal); |
| 102 | +} |
| 103 | + |
| 104 | +void ASTSrcLocProcessor::generate() { |
| 105 | + WriteJSON(JsonPath, ClassInheritance, ClassesInClade, ClassEntries); |
| 106 | +} |
| 107 | + |
| 108 | +std::vector<std::string> |
| 109 | +CaptureMethods(std::string TypeString, const clang::CXXRecordDecl *ASTClass, |
| 110 | + const MatchFinder::MatchResult &Result) { |
| 111 | + |
| 112 | + auto publicAccessor = [](auto... InnerMatcher) { |
| 113 | + return cxxMethodDecl(isPublic(), parameterCountIs(0), isConst(), |
| 114 | + InnerMatcher...); |
| 115 | + }; |
| 116 | + |
| 117 | + auto BoundNodesVec = |
| 118 | + match(findAll(publicAccessor(ofClass(equalsNode(ASTClass)), |
| 119 | + returns(asString(TypeString))) |
| 120 | + .bind("classMethod")), |
| 121 | + *ASTClass, *Result.Context); |
| 122 | + |
| 123 | + std::vector<std::string> Methods; |
| 124 | + for (const auto &BN : BoundNodesVec) { |
| 125 | + if (const auto *Node = BN.getNodeAs<clang::NamedDecl>("classMethod")) { |
| 126 | + // Only record the getBeginLoc etc on Stmt etc, because it will call |
| 127 | + // more-derived implementations pseudo-virtually. |
| 128 | + if ((ASTClass->getName() != "Stmt" && ASTClass->getName() != "Decl") && |
| 129 | + (Node->getName() == "getBeginLoc" || Node->getName() == "getEndLoc" || |
| 130 | + Node->getName() == "getSourceRange")) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + // Only record the getExprLoc on Expr, because it will call |
| 134 | + // more-derived implementations pseudo-virtually. |
| 135 | + if (ASTClass->getName() != "Expr" && Node->getName() == "getExprLoc") { |
| 136 | + continue; |
| 137 | + } |
| 138 | + Methods.push_back(Node->getName().str()); |
| 139 | + } |
| 140 | + } |
| 141 | + return Methods; |
| 142 | +} |
| 143 | + |
| 144 | +void ASTSrcLocProcessor::run(const MatchFinder::MatchResult &Result) { |
| 145 | + |
| 146 | + if (const auto *ASTClass = |
| 147 | + Result.Nodes.getNodeAs<clang::CXXRecordDecl>("className")) { |
| 148 | + |
| 149 | + StringRef ClassName = ASTClass->getName(); |
| 150 | + |
| 151 | + ClassData CD; |
| 152 | + |
| 153 | + const auto *NodeClade = |
| 154 | + Result.Nodes.getNodeAs<clang::CXXRecordDecl>("nodeClade"); |
| 155 | + StringRef CladeName = NodeClade->getName(); |
| 156 | + |
| 157 | + if (const auto *DerivedFrom = |
| 158 | + Result.Nodes.getNodeAs<clang::CXXRecordDecl>("derivedFrom")) |
| 159 | + ClassInheritance[ClassName] = DerivedFrom->getName(); |
| 160 | + |
| 161 | + CD.ASTClassLocations = |
| 162 | + CaptureMethods("class clang::SourceLocation", ASTClass, Result); |
| 163 | + CD.ASTClassRanges = |
| 164 | + CaptureMethods("class clang::SourceRange", ASTClass, Result); |
| 165 | + |
| 166 | + if (!CD.isEmpty()) { |
| 167 | + ClassEntries[ClassName] = CD; |
| 168 | + ClassesInClade[CladeName].push_back(ClassName); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments