12#include "llvm/Support/JSON.h"
13#include "llvm/Support/MemoryBuffer.h"
20 : JsonPath(JsonPath) {
24 Finder = std::make_unique<MatchFinder>(std::move(FinderOptions));
31 "clang::Stmt",
"clang::Decl",
"clang::CXXCtorInitializer",
32 "clang::NestedNameSpecifierLoc",
33 "clang::TemplateArgumentLoc",
"clang::CXXBaseSpecifier",
34 "clang::DeclarationNameInfo",
"clang::TypeLoc"))
41 "clang::TypeofLikeTypeLoc"))
42 .bind(
"templateName"),
46std::unique_ptr<clang::ASTConsumer>
49 return Finder->newASTConsumer();
52llvm::json::Object
toJSON(llvm::StringMap<std::vector<StringRef>>
const &Obj) {
53 using llvm::json::toJSON;
55 llvm::json::Object JsonObj;
56 for (
const auto &Item : Obj) {
57 JsonObj[Item.first()] = Item.second;
62llvm::json::Object
toJSON(llvm::StringMap<std::string>
const &Obj) {
63 using llvm::json::toJSON;
65 llvm::json::Object JsonObj;
66 for (
const auto &Item : Obj) {
67 JsonObj[Item.first()] = Item.second;
73 llvm::json::Object JsonObj;
92llvm::json::Object
toJSON(llvm::StringMap<ClassData>
const &Obj) {
93 using llvm::json::toJSON;
95 llvm::json::Object JsonObj;
96 for (
const auto &Item : Obj)
97 JsonObj[Item.first()] =
::toJSON(Item.second);
101void WriteJSON(StringRef JsonPath, llvm::json::Object &&ClassInheritance,
102 llvm::json::Object &&ClassesInClade,
103 llvm::json::Object &&ClassEntries) {
104 llvm::json::Object JsonObj;
106 using llvm::json::toJSON;
108 JsonObj[
"classInheritance"] = std::move(ClassInheritance);
109 JsonObj[
"classesInClade"] = std::move(ClassesInClade);
110 JsonObj[
"classEntries"] = std::move(ClassEntries);
112 llvm::json::Value JsonVal(std::move(JsonObj));
114 bool WriteChange =
false;
115 std::string OutString;
116 if (
auto ExistingOrErr = MemoryBuffer::getFile(JsonPath,
true)) {
117 raw_string_ostream Out(OutString);
118 Out << formatv(
"{0:2}", JsonVal);
119 if (ExistingOrErr.get()->getBuffer() == Out.str())
125 llvm::raw_fd_ostream JsonOut(JsonPath, EC, llvm::sys::fs::OF_Text);
130 JsonOut << OutString;
132 JsonOut << formatv(
"{0:2}", JsonVal);
142std::vector<std::string>
146 auto publicAccessor = [](
auto... InnerMatcher) {
147 return cxxMethodDecl(isPublic(), parameterCountIs(0), isConst(),
151 auto BoundNodesVec =
match(
155 equalsNode(ASTClass),
158 .bind(
"stmtOrDeclBase"))),
163 .bind(
"typeLocBase"))))),
164 returns(hasCanonicalType(asString(TypeString))))
165 .bind(
"classMethod")),
166 *ASTClass, *
Result.Context);
168 std::vector<std::string> Methods;
169 for (
const auto &BN : BoundNodesVec) {
171 const auto *StmtOrDeclBase =
173 const auto *TypeLocBase =
180 if (StmtOrDeclBase &&
181 (
Node->getName() ==
"getBeginLoc" ||
Node->getName() ==
"getEndLoc" ||
182 Node->getName() ==
"getSourceRange"))
184 if (ExprBase &&
Node->getName() ==
"getExprLoc")
186 if (TypeLocBase &&
Node->getName() ==
"getLocalSourceRange")
188 if ((ASTClass->
getName() ==
"PointerLikeTypeLoc" ||
189 ASTClass->
getName() ==
"TypeofLikeTypeLoc") &&
190 Node->getName() ==
"getLocalSourceRange")
192 Methods.push_back(
Node->getName().str());
200 const auto *ASTClass =
205 if (
const auto *NodeClade =
207 CladeName = NodeClade->
getName();
210 CladeName =
"TypeLoc";
213 StringRef ClassName = ASTClass->getName();
228 auto DI =
CaptureMethods(
"const struct clang::DeclarationNameInfo &",
232 if (
const auto *DerivedFrom =
235 if (
const auto *Templ =
236 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
239 const auto &TArgs = Templ->getTemplateArgs();
242 llvm::raw_svector_ostream OS(TArgsString);
243 OS << DerivedFrom->getName() <<
'<';
246 PPol.TerseOutput =
true;
248 for (
unsigned I = 0; I < TArgs.size(); ++I) {
251 TArgs.get(I).getAsType().print(OS, PPol);
255 ClassInheritance[ClassName] = TArgsString.str().str();
257 ClassInheritance[ClassName] = DerivedFrom->getName().str();
261 if (
const auto *Templ = ASTClass->getDescribedClassTemplate()) {
262 if (
auto *TParams = Templ->getTemplateParameters()) {
263 for (
const auto &TParam : *TParams) {
269 ClassEntries[ClassName] = CD;
270 ClassesInClade[CladeName].push_back(ClassName);
void WriteJSON(StringRef JsonPath, llvm::json::Object &&ClassInheritance, llvm::json::Object &&ClassesInClade, llvm::json::Object &&ClassEntries)
llvm::json::Object toJSON(llvm::StringMap< std::vector< StringRef > > const &Obj)
std::vector< std::string > CaptureMethods(std::string TypeString, const clang::CXXRecordDecl *ASTClass, const MatchFinder::MatchResult &Result)
Represents a C++ struct/union/class.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
This represents a decl that may have a name.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
const internal::VariadicOperatorMatcherFunc< 1, 1 > optionally
Matches any node regardless of the submatcher.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::VariadicDynCastAllOfMatcher< Decl, NamedDecl > namedDecl
Matches a declaration of anything that could have a name.
const internal::VariadicFunction< internal::Matcher< NamedDecl >, StringRef, internal::hasAnyNameFunc > hasAnyName
Matches NamedDecl nodes that have any of the specified names.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
@ Result
The result type of a method or function.
Diagnostic wrappers for TextAPI types for error reporting.
Describes how types, statements, expressions, and declarations should be printed.
Contains all information for a given match.