-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathHelloWorld.cpp
114 lines (98 loc) · 4.09 KB
/
HelloWorld.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//==============================================================================
// FILE:
// HelloWorld.cpp
//
// DESCRIPTION:
// Counts the number of C++ record declarations in the input translation
// unit. The results are printed on a file-by-file basis (i.e. for each
// included header file separately).
//
// Internally, this implementation leverages llvm::StringMap to map file
// names to the corresponding #count of declarations.
//
// USAGE:
// clang -cc1 -load <BUILD_DIR>/lib/libHelloWorld.dylib '\'
// -plugin hello-world test/HelloWorld-basic.cpp
//
// License: The Unlicense
//==============================================================================
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
//-----------------------------------------------------------------------------
// RecursiveASTVisitor
//-----------------------------------------------------------------------------
class HelloWorld : public RecursiveASTVisitor<HelloWorld> {
public:
explicit HelloWorld(ASTContext *Context) : Context(Context) {}
bool VisitCXXRecordDecl(CXXRecordDecl *Decl);
llvm::StringMap<unsigned> getDeclMap() { return DeclMap; }
private:
ASTContext *Context;
// Map that contains the count of declaration in every input file
llvm::StringMap<unsigned> DeclMap;
};
bool HelloWorld::VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getBeginLoc());
// Basic sanity checking
if (!FullLocation.isValid())
return true;
// There are 2 types of source locations: in a file or a macro expansion. The
// latter contains the spelling location and the expansion location (both are
// file locations), but only the latter is needed here (i.e. where the macro
// is expanded). File locations are just that - file locations.
if (FullLocation.isMacroID())
FullLocation = FullLocation.getExpansionLoc();
SourceManager &SrcMgr = Context->getSourceManager();
OptionalFileEntryRef Entry =
SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(FullLocation));
DeclMap[Entry->getName()]++;
return true;
}
//-----------------------------------------------------------------------------
// ASTConsumer
//-----------------------------------------------------------------------------
class HelloWorldASTConsumer : public clang::ASTConsumer {
public:
explicit HelloWorldASTConsumer(ASTContext *Ctx) : Visitor(Ctx) {}
void HandleTranslationUnit(clang::ASTContext &Ctx) override {
Visitor.TraverseDecl(Ctx.getTranslationUnitDecl());
if (Visitor.getDeclMap().empty()) {
llvm::outs() << "(clang-tutor) no declarations found "
<< "\n";
return;
}
for (auto &Element : Visitor.getDeclMap()) {
llvm::outs() << "(clang-tutor) file: " << Element.first() << "\n";
llvm::outs() << "(clang-tutor) count: " << Element.second << "\n";
}
}
private:
HelloWorld Visitor;
};
//-----------------------------------------------------------------------------
// FrontendAction for HelloWorld
//-----------------------------------------------------------------------------
class FindNamedClassAction : public clang::PluginASTAction {
public:
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler,
llvm::StringRef InFile) override {
return std::unique_ptr<clang::ASTConsumer>(
std::make_unique<HelloWorldASTConsumer>(&Compiler.getASTContext()));
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) override {
return true;
}
};
//-----------------------------------------------------------------------------
// Registration
//-----------------------------------------------------------------------------
static FrontendPluginRegistry::Add<FindNamedClassAction>
X(/*Name=*/"hello-world", /*Description=*/"The HelloWorld plugin");