OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This clang tool finds all instances of net::DefineNetworkTrafficAnnotation in | |
6 // given source code, extracts the location info and content of annotation tags | |
7 // (unique id and annotation text), and stores them in separate text files | |
8 // (per instance) in the given output directory. Please refer to README.md for | |
9 // build and usage instructions. | |
10 | |
11 #include <stdio.h> | |
12 #include <fstream> | |
13 #include <memory> | |
14 | |
15 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
16 #include "clang/ASTMatchers/ASTMatchers.h" | |
17 #include "clang/Basic/SourceManager.h" | |
18 #include "clang/Frontend/FrontendActions.h" | |
19 #include "clang/Lex/Lexer.h" | |
20 #include "clang/Tooling/CommonOptionsParser.h" | |
21 #include "clang/Tooling/Refactoring.h" | |
22 #include "clang/Tooling/Tooling.h" | |
23 #include "llvm/Support/CommandLine.h" | |
24 | |
25 using namespace clang::ast_matchers; | |
26 | |
27 namespace { | |
28 | |
29 // An instance of a call to the net::DefineNetworkTrafficAnnotation function. | |
30 struct NetworkAnnotationInstance { | |
31 // Information about where the call has happened. | |
32 struct Location { | |
33 std::string file_path; | |
34 int line_number = -1; | |
35 | |
36 // Name of the function calling net::DefineNetworkTrafficAnnotation. E.g., | |
37 // in the following code, |function_name| will be 'foo': | |
38 // void foo() { NetworkTrafficAnnotationTag bar = | |
39 // net::DefineNetworkTrafficAnnotation(...); } | |
40 std::string function_name; | |
41 }; | |
42 | |
43 // Annotation content. These are the parameters of the call to | |
44 // net::DefineNetworkTrafficAnnotation. The unique_id is an identifier for the | |
45 // annotation that has to be unique across the entire code base. The |text| | |
46 // stores a raw string with the annotation that should be extracted. | |
47 struct Annotation { | |
48 std::string unique_id; | |
49 std::string text; | |
50 }; | |
51 | |
52 Location location; | |
53 Annotation annotation; | |
54 }; | |
55 | |
56 using Collector = std::vector<NetworkAnnotationInstance>; | |
57 | |
58 // Returns the function that contains the given token. For example, if the token | |
59 // is variable x in the code "void foo() { int x; ... }", it returns "foo". | |
60 llvm::StringRef GetDeclarationCoveringFunction(const clang::Decl* token, | |
dcheng
2017/04/11 07:26:42
It's usually nicer to express things as matchers w
Ramin Halavati
2017/04/11 09:15:38
WOW! That was wonderful.
| |
61 clang::ASTContext* context); | |
62 | |
63 // Returns the function that contains the given token. For example, if the token | |
64 // is the call to function bar() in the code "void foo() { bar(); }", it returns | |
65 // "foo". | |
66 llvm::StringRef GetStatementCoveringFunction(const clang::Stmt* token, | |
67 clang::ASTContext* context) { | |
68 // Get the parent of |token| and return its covering function. | |
69 clang::ASTContext::DynTypedNodeList parents = context->getParents(*token); | |
70 | |
71 // If parent is found, extract its name recursively. | |
72 if (parents.size()) { | |
73 if (const clang::Stmt* s = parents[0].get<clang::Stmt>()) | |
74 return GetStatementCoveringFunction(s, context); | |
75 else if (const clang::Decl* d = parents[0].get<clang::Decl>()) | |
76 return GetDeclarationCoveringFunction(d, context); | |
77 } | |
78 | |
79 return llvm::StringRef("Unknown"); | |
80 } | |
81 | |
82 // Returns the function that contains the given token. For example, if the token | |
83 // is variable x in the code "void foo() { int x; ... }", it returns "foo". | |
84 llvm::StringRef GetDeclarationCoveringFunction(const clang::Decl* token, | |
85 clang::ASTContext* context) { | |
86 // If |token| is a function declaration, return its name. | |
87 if (auto f = clang::dyn_cast<clang::FunctionDecl>(token)) | |
88 return f->getQualifiedNameAsString(); | |
89 | |
90 // As |token| is not a function declaration, get its parent and return its | |
91 // covering function. | |
92 clang::ASTContext::DynTypedNodeList parents = context->getParents(*token); | |
93 | |
94 // If parent is found, extract its name recursively. | |
95 if (parents.size()) { | |
96 if (const clang::Stmt* s = parents[0].get<clang::Stmt>()) | |
97 return GetStatementCoveringFunction(s, context); | |
98 else if (const clang::Decl* d = parents[0].get<clang::Decl>()) | |
99 return GetDeclarationCoveringFunction(d, context); | |
100 } | |
101 | |
102 return llvm::StringRef("Unknown"); | |
dcheng
2017/04/11 07:26:42
Btw, no need to explicit construct StringRef here.
Ramin Halavati
2017/04/11 09:15:38
Done.
| |
103 } | |
104 | |
105 // This class implements the call back functions for AST Matchers. The matchers | |
106 // are defined in RunMatchers function. When a pattern is found there, | |
107 // the run function in this class is called back with information on the matched | |
108 // location and description of the matched pattern. | |
109 class NetworkAnnotationTagCallback : public MatchFinder::MatchCallback { | |
110 public: | |
111 explicit NetworkAnnotationTagCallback(Collector* collector) | |
112 : collector_(collector) {} | |
113 ~NetworkAnnotationTagCallback() override = default; | |
114 | |
115 // Is called on any pattern found by ASTMathers that are defined in RunMathers | |
116 // function. | |
117 virtual void run(const MatchFinder::MatchResult& result) override { | |
118 const clang::CallExpr* call_expr = | |
119 result.Nodes.getNodeAs<clang::CallExpr>("definition_function"); | |
120 const clang::StringLiteral* unique_id = | |
121 result.Nodes.getNodeAs<clang::StringLiteral>("unique_id"); | |
122 const clang::StringLiteral* annotation_text = | |
123 result.Nodes.getNodeAs<clang::StringLiteral>("annotation_text"); | |
124 | |
125 if (call_expr && unique_id && annotation_text) { | |
dcheng
2017/04/11 07:26:42
Let's assert since we should expect all these to b
Ramin Halavati
2017/04/11 09:15:38
Done.
| |
126 NetworkAnnotationInstance instance; | |
127 instance.annotation.unique_id = unique_id->getString(); | |
128 instance.annotation.text = annotation_text->getString(); | |
129 | |
130 // Get annotation location. | |
131 clang::SourceLocation source_location = call_expr->getLocStart(); | |
132 instance.location.file_path = | |
133 result.SourceManager->getFilename(source_location); | |
134 instance.location.line_number = | |
135 result.SourceManager->getSpellingLineNumber(source_location); | |
136 instance.location.function_name = GetStatementCoveringFunction( | |
137 clang::dyn_cast<clang::Stmt>(call_expr), result.Context); | |
138 | |
139 // If DefineNetworkTrafficAnnotation is used in form of a macro, an empty | |
140 // file_path is returned. Traversing to its parent node in this case | |
141 // will result in the correct value. | |
142 if (!instance.location.file_path.length()) { | |
dcheng
2017/04/11 07:26:42
Can we do something with getImmediateMacroCallerLo
Ramin Halavati
2017/04/11 09:15:38
Done.
| |
143 instance.location.file_path = "unknown_file_path"; | |
144 clang::ASTContext::DynTypedNodeList parents = | |
145 result.Context->getParents( | |
146 *clang::dyn_cast<clang::Stmt>(call_expr)); | |
147 if (parents.size()) { | |
148 if (const clang::Stmt* s = parents[0].get<clang::Stmt>()) { | |
149 source_location = s->getLocStart(); | |
150 instance.location.file_path = | |
151 result.SourceManager->getFilename(source_location); | |
152 instance.location.line_number = | |
153 result.SourceManager->getSpellingLineNumber(source_location); | |
154 } | |
155 } | |
156 } | |
157 | |
158 // Remove leading "../"s from file path. | |
159 while (instance.location.file_path.length() > 3 && | |
160 instance.location.file_path.substr(0, 3) == "../") { | |
dcheng
2017/04/11 07:26:42
I would be cautious of doing this, as Windows will
Ramin Halavati
2017/04/11 09:15:38
I did the replacement, but I am not sure if I got
| |
161 instance.location.file_path = instance.location.file_path.substr( | |
162 3, instance.location.file_path.length() - 3); | |
163 } | |
164 | |
165 collector_->push_back(instance); | |
166 } | |
167 } | |
168 | |
169 private: | |
170 Collector* collector_; | |
171 }; | |
172 | |
173 // Sets up an ASTMatcher and runs clang tool to populate collector. Returns the | |
174 // result of running the clang tool. | |
175 int RunMatchers(clang::tooling::ClangTool* clang_tool, Collector* collector) { | |
176 NetworkAnnotationTagCallback call_back(collector); | |
177 MatchFinder match_finder; | |
178 | |
179 // Set up a pattern to find functions that are named | |
180 // [net::]DefineNetworkTrafficAnnotation and have 2 arguments of string | |
181 // literal type. | |
182 match_finder.addMatcher( | |
183 callExpr(hasDeclaration(functionDecl( | |
184 anyOf(hasName("DefineNetworkTrafficAnnotation"), | |
185 hasName("net::DefineNetworkTrafficAnnotation")))), | |
186 hasArgument(0, stringLiteral().bind("unique_id")), | |
187 hasArgument(1, stringLiteral().bind("annotation_text"))) | |
188 .bind("definition_function"), | |
189 &call_back); | |
190 std::unique_ptr<clang::tooling::FrontendActionFactory> frontend_factory = | |
191 clang::tooling::newFrontendActionFactory(&match_finder); | |
192 return clang_tool->run(frontend_factory.get()); | |
193 } | |
194 | |
195 } // namespace | |
196 | |
197 static llvm::cl::OptionCategory ToolCategory( | |
198 "traffic_annotation_extractor: Extract traffic annotation texts"); | |
199 static llvm::cl::extrahelp CommonHelp( | |
200 clang::tooling::CommonOptionsParser::HelpMessage); | |
201 static llvm::cl::extrahelp MoreHelp( | |
202 "\n -output-directory=<string> - The directory in which the output " | |
203 "files are written.\n"); | |
204 static llvm::cl::opt<std::string> OutputDirectory( | |
205 "output-directory", | |
206 llvm::cl::desc("The directory in which output files are written.\n")); | |
207 | |
208 int main(int argc, const char* argv[]) { | |
209 clang::tooling::CommonOptionsParser options(argc, argv, ToolCategory); | |
210 clang::tooling::ClangTool tool(options.getCompilations(), | |
211 options.getSourcePathList()); | |
212 Collector collector; | |
213 | |
214 int result = RunMatchers(&tool, &collector); | |
215 | |
216 if (result != 0) | |
217 return result; | |
218 | |
219 // For each call to "DefineNetworkTrafficAnnotation", write annotation text | |
dcheng
2017/04/11 07:26:42
Btw, I'm curious if we still need to do this: for
Ramin Halavati
2017/04/11 09:15:38
Thanks, Done!
| |
220 // and relevant meta data into a separate file. The filename is uniquely | |
221 // generated using the file path and filename of the code including the call | |
222 // and it's line number. | |
dcheng
2017/04/11 07:26:42
Nit: it's -> its
Ramin Halavati
2017/04/11 09:15:38
Done.
| |
223 for (NetworkAnnotationInstance& call : collector) { | |
224 std::string s = call.location.file_path; | |
225 std::replace(s.begin(), s.end(), '/', '_'); | |
226 std::replace(s.begin(), s.end(), '.', '_'); | |
227 std::string file_path = OutputDirectory.getValue() + "/" + s + "(" + | |
228 std::to_string(call.location.line_number) + ").txt"; | |
229 | |
230 std::ofstream output_file(file_path); | |
231 if (output_file.is_open()) { | |
232 output_file << call.location.file_path << "\n"; | |
233 output_file << call.location.function_name << "\n"; | |
234 output_file << call.location.line_number << "\n"; | |
235 output_file << call.annotation.unique_id << "\n"; | |
236 output_file << call.annotation.text << "\n"; | |
237 output_file.close(); | |
238 } else { | |
239 llvm::errs() << "Could not write to file: " << file_path << "\n"; | |
240 return 1; | |
241 } | |
242 } | |
243 | |
244 return 0; | |
245 } | |
OLD | NEW |