-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClangStringDict.cpp
175 lines (155 loc) · 6.66 KB
/
ClangStringDict.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Regex.h"
using namespace clang::tooling;
using namespace llvm;
using namespace clang;
using namespace clang::ast_matchers;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static cl::OptionCategory MyToolCategory("clang-sdict options");
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nTakes a compilation database and spits out CString Literals in source files\n");
// nDPI specific matcher
StatementMatcher StrcmpMatcher = stringLiteral(
hasAncestor(
callExpr(
hasDescendant(
declRefExpr(
hasDeclaration(
namedDecl(anyOf(
hasName("strcmp"),
hasName("__builtin_strcmp"),
hasName("strncmp"),
hasName("__builtin_strncmp"),
hasName("memcmp"),
hasName("__builtin_memcmp"),
hasName("memmove"),
hasName("__builtin_memmove"),
hasName("unaligned_memcpy"),
hasName("unaligned_memcmp")
)
))))))).bind("strcmp");
// libxml2 specific matcher
StatementMatcher xmlcharMatcher = stringLiteral(
hasAncestor(
callExpr(
hasDescendant(
declRefExpr(
hasDeclaration(
namedDecl(anyOf(
hasName("xmlBufferWriteChar"),
hasName("xmlOutputBufferWrite")
)
)
)
)
)
)
)
).bind("xml");
// woff2 specific matcher
DeclarationMatcher woff2constintMatcher = varDecl(
allOf(
hasType(isConstQualified()),
hasDescendant(integerLiteral().bind("woff2int")))
);
// re2 specific matcher
StatementMatcher re2charlitMatcher = characterLiteral(
hasAncestor(
cxxMethodDecl()
)
).bind("re2char");
// Generic
StatementMatcher IntLitMatcher = integerLiteral(anyOf(
hasParent(binaryOperator(hasAncestor(ifStmt()))),
hasAncestor(caseStmt())
// hasParent(callExpr())
)).bind("intlit");
StatementMatcher StrLitMatcher = stringLiteral(anyOf(
hasParent(binaryOperator(hasAncestor(ifStmt()))),
hasParent(caseStmt())
// hasParent(callExpr())
)).bind("strlit");
StatementMatcher CharLitMatcher = characterLiteral(anyOf(
hasParent(binaryOperator(hasAncestor(ifStmt()))),
hasParent(caseStmt())
// hasParent(callExpr())
)).bind("charlit");
class MatchPrinter : public MatchFinder::MatchCallback {
public :
void printToken(StringRef token) {
size_t tokenlen = token.size();
if ((tokenlen == 0) || (tokenlen > 128))
return;
llvm::outs() << "\"" + token + "\"" << "\n";
}
void prettyPrintIntString(std::string inString) {
if (inString.empty())
return;
#if 1
size_t inStrLen = inString.size();
if (inStrLen % 2) {
inString.insert(0, "0");
inStrLen++;
}
for (size_t i = 0; i < (2 * inStrLen); i+=4) {
inString.insert(i, "\\x");
}
#else
inString.insert(0, "0x");
#endif
printToken(inString);
}
void formatIntLiteral(const IntegerLiteral *IL) {
std::string inString = IL->getValue().toString(16, false);
prettyPrintIntString(inString);
}
void formatCharLiteral(const CharacterLiteral *CL) {
unsigned value = CL->getValue();
std::string valString = llvm::APInt(8, value).toString(16, false);
prettyPrintIntString(valString);
}
virtual void run(const MatchFinder::MatchResult &Result) {
if (const clang::StringLiteral *SL = Result.Nodes.getNodeAs<clang::StringLiteral>("strcmp"))
printToken(SL->getString());
if (const clang::StringLiteral *SL = Result.Nodes.getNodeAs<clang::StringLiteral>("strlit"))
printToken(SL->getString());
if (const clang::IntegerLiteral *IL = Result.Nodes.getNodeAs<clang::IntegerLiteral>("intlit"))
formatIntLiteral(IL);
if (const clang::StringLiteral *SL = Result.Nodes.getNodeAs<clang::StringLiteral>("xml"))
printToken(SL->getString());
if (const clang::IntegerLiteral *IL = Result.Nodes.getNodeAs<clang::IntegerLiteral>("woff2int"))
formatIntLiteral(IL);
if (const clang::CharacterLiteral *CL = Result.Nodes.getNodeAs<clang::CharacterLiteral>("charlit"))
formatCharLiteral(CL);
}
};
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
MatchPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(IntLitMatcher, &Printer);
Finder.addMatcher(StrLitMatcher, &Printer);
Finder.addMatcher(CharLitMatcher, &Printer);
Finder.addMatcher(StrcmpMatcher, &Printer);
// Finder.addMatcher(xmlcharMatcher, &Printer);
// Finder.addMatcher(woff2constintMatcher, &Printer);
return Tool.run(newFrontendActionFactory(&Finder).get());
}