Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: tools/clang/traffic_annotation_extractor/traffic_annotation_extractor.cpp

Issue 2448133006: Tool added to extract network traffic annotations. (Closed)
Patch Set: nits Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 call to net::DefineNetworkTrafficAnnotation function.
battre 2017/04/07 08:42:28 An instance of *a* to *the* ... function.
Ramin Halavati 2017/04/07 11:33:32 Done.
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 including this instance. E.g., in the following
battre 2017/04/07 08:42:27 I don't understand "Name of the function including
Ramin Halavati 2017/04/07 11:33:32 Done. Namespaces are ignored and if DefineNetwork
37 // 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 a call to
battre 2017/04/07 08:42:27 of the call
Ramin Halavati 2017/04/07 11:33:32 Done.
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.
battre 2017/04/07 08:42:28 raw
Ramin Halavati 2017/04/07 11:33:32 Done.
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 includes the given token. For example, if the token
battre 2017/04/07 08:42:28 s/includes/contains/
Ramin Halavati 2017/04/07 11:33:32 Done.
59 // is variable x in the code "void foo() { int x; ... }", it returns "foo".
60 llvm::StringRef GetDeclarationCoveringFunction(const clang::Decl* token,
61 clang::ASTContext* context);
62
63 // Returns the function that includes the given token. For example, if the token
battre 2017/04/07 08:42:27 s/includes/contains/
Ramin Halavati 2017/04/07 11:33:32 Done.
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.
battre 2017/04/07 08:42:28 Can you explain this to me? In which case would we
Ramin Halavati 2017/04/07 11:33:32 Frankly I don't know and I couldn't find any docum
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 includes the given token. For example, if the token
battre 2017/04/07 08:42:27 contains
Ramin Halavati 2017/04/07 11:33:32 Done.
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");
103 }
104
105 // This class implements the call back functions for AST Matchers. The matchers
106 // are defined in RunMatchers function and when a pattern is found there,
battre 2017/04/07 08:42:28 "function. When a" ...
Ramin Halavati 2017/04/07 11:33:32 Done.
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) {
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 collector_->push_back(instance);
140 }
141 }
142
143 private:
144 Collector* collector_;
145 };
146
147 // Sets up an ASTMatcher and runs clang tool to populate collector. Returns the
148 // result of running the clang tool.
149 int RunMatchers(clang::tooling::ClangTool* clang_tool, Collector* collector) {
150 NetworkAnnotationTagCallback call_back(collector);
151 MatchFinder match_finder;
152
153 // Set up a pattern to find functions that are named
154 // [net::]DefineNetworkTrafficAnnotation and have 2 arguments of string
155 // literal type.
156 match_finder.addMatcher(
157 callExpr(hasDeclaration(functionDecl(
158 anyOf(hasName("DefineNetworkTrafficAnnotation"),
159 hasName("net::DefineNetworkTrafficAnnotation")))),
160 hasArgument(0, stringLiteral().bind("unique_id")),
161 hasArgument(1, stringLiteral().bind("annotation_text")))
162 .bind("definition_function"),
163 &call_back);
164 std::unique_ptr<clang::tooling::FrontendActionFactory> frontend_factory =
165 clang::tooling::newFrontendActionFactory(&match_finder);
166 return clang_tool->run(frontend_factory.get());
167 }
168
169 } // namespace
170
171 static llvm::cl::OptionCategory ToolCategory(
172 "traffic_annotation_extractor: Extract traffic annotation texts");
173 static llvm::cl::extrahelp CommonHelp(
174 clang::tooling::CommonOptionsParser::HelpMessage);
175 static llvm::cl::extrahelp MoreHelp(
176 "\n -output-directory=<string> - The directory in which the output "
177 "files are written.\n");
178 static llvm::cl::opt<std::string> OutputDirectory(
179 "output-directory",
180 llvm::cl::desc("The directory in which output files are written.\n"));
181
182 int main(int argc, const char* argv[]) {
183 clang::tooling::CommonOptionsParser options(argc, argv, ToolCategory);
184 clang::tooling::ClangTool tool(options.getCompilations(),
185 options.getSourcePathList());
186 Collector collector;
187
188 int result = RunMatchers(&tool, &collector);
189
190 if (result != 0)
191 return result;
192
193 // For each call to "DefineNetworkTrafficAnnotation", write annotation text
194 // and relevant meta data into a separate file. The filename is uniquely
195 // generated using the file path and filename of the code including the call
196 // and it's line number.
197 for (NetworkAnnotationInstance& call : collector) {
198 std::string s = call.location.file_path;
199 std::replace(s.begin(), s.end(), '/', '_');
200 std::replace(s.begin(), s.end(), '.', '_');
201 std::string file_path = OutputDirectory.getValue() + "/" + s + "(" +
202 std::to_string(call.location.line_number) + ").txt";
203
204 std::ofstream output_file(file_path);
205 if (output_file.is_open()) {
206 output_file << call.location.file_path << "\n";
207 output_file << call.location.function_name << "\n";
208 output_file << call.location.line_number << "\n";
209 output_file << call.annotation.unique_id << "\n";
210 output_file << call.annotation.text << "\n";
211 output_file.close();
212 } else {
213 llvm::errs() << "Could not write to file: " << file_path << "\n";
214 return 1;
215 }
216 }
217
218 return 0;
219 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698