1 // Copyright (C) 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "dumper/header_checker.h"
16
17 #include "dumper/fixed_argv.h"
18 #include "dumper/frontend_action_factory.h"
19 #include "utils/command_line_utils.h"
20 #include "utils/header_abi_util.h"
21
22 #include <clang/Driver/Driver.h>
23 #include <clang/Frontend/FrontendActions.h>
24 #include <clang/Tooling/CommonOptionsParser.h>
25 #include <clang/Tooling/CompilationDatabase.h>
26 #include <clang/Tooling/Tooling.h>
27 #include <llvm/Support/CommandLine.h>
28 #include <llvm/Support/FileSystem.h>
29 #include <llvm/Support/raw_ostream.h>
30
31 #include <memory>
32 #include <string>
33 #include <vector>
34
35 #include <stdlib.h>
36
37
38 using header_checker::dumper::FixedArgv;
39 using header_checker::dumper::FixedArgvAccess;
40 using header_checker::dumper::FixedArgvRegistry;
41 using header_checker::dumper::HeaderCheckerFrontendActionFactory;
42 using header_checker::dumper::HeaderCheckerOptions;
43 using header_checker::repr::TextFormatIR;
44 using header_checker::utils::CollectAllExportedHeaders;
45 using header_checker::utils::HideIrrelevantCommandLineOptions;
46 using header_checker::utils::NormalizePath;
47 using header_checker::utils::ParseRootDirs;
48 using header_checker::utils::RootDir;
49 using header_checker::utils::RootDirs;
50
51
52 static llvm::cl::OptionCategory header_checker_category(
53 "header-checker options");
54
55 static llvm::cl::opt<std::string> header_file(
56 llvm::cl::Positional, llvm::cl::desc("<source.cpp>"), llvm::cl::Optional,
57 llvm::cl::cat(header_checker_category));
58
59 static llvm::cl::opt<std::string> out_dump(
60 "o", llvm::cl::value_desc("out_dump"), llvm::cl::Optional,
61 llvm::cl::desc("Specify the reference dump file name"),
62 llvm::cl::cat(header_checker_category));
63
64 static llvm::cl::list<std::string> exported_header_dirs(
65 "I", llvm::cl::desc("<export_include_dirs>"), llvm::cl::Prefix,
66 llvm::cl::ZeroOrMore, llvm::cl::cat(header_checker_category));
67
68 static llvm::cl::list<std::string> root_dirs(
69 "root-dir",
70 llvm::cl::desc("Specify the directory that the paths in the dump files "
71 "are relative to. The format is <path>:<replacement> or "
72 "<path>. If this option is not specified, it defaults to "
73 "current working directory."),
74 llvm::cl::ZeroOrMore, llvm::cl::cat(header_checker_category));
75
76 static llvm::cl::opt<bool> no_filter(
77 "no-filter", llvm::cl::desc("Do not filter any abi"), llvm::cl::Optional,
78 llvm::cl::cat(header_checker_category));
79
80 static llvm::cl::opt<bool> dump_function_declarations(
81 "dump-function-declarations",
82 llvm::cl::desc("Output the functions declared but not defined in the input "
83 "file"),
84 llvm::cl::Optional, llvm::cl::cat(header_checker_category));
85
86 static llvm::cl::opt<TextFormatIR> output_format(
87 "output-format", llvm::cl::desc("Specify format of output dump file"),
88 llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
89 "ProtobufTextFormat", "ProtobufTextFormat"),
90 clEnumValN(TextFormatIR::Json, "Json", "JSON")),
91 llvm::cl::init(TextFormatIR::Json),
92 llvm::cl::cat(header_checker_category));
93
94 static llvm::cl::opt<bool> print_resource_dir(
95 "print-resource-dir",
96 llvm::cl::desc("Print real path to default resource directory"),
97 llvm::cl::Optional, llvm::cl::cat(header_checker_category));
98
99 int main(int argc, const char **argv);
100
PrintResourceDir(const char * argv_0)101 static bool PrintResourceDir(const char *argv_0) {
102 std::string program_path =
103 llvm::sys::fs::getMainExecutable(argv_0, (void *)main);
104 if (program_path.empty()) {
105 llvm::errs() << "Failed to get program path\n";
106 return false;
107 }
108 llvm::outs() << clang::driver::Driver::GetResourcesPath(program_path) << "\n";
109 return true;
110 }
111
main(int argc,const char ** argv)112 int main(int argc, const char **argv) {
113 HideIrrelevantCommandLineOptions(header_checker_category);
114
115 // Tweak argc and argv to workaround clang version mismatches.
116 FixedArgv fixed_argv(argc, argv);
117 FixedArgvRegistry::Apply(fixed_argv);
118
119 // Create compilation database from command line arguments after "--".
120 std::string cmdline_error_msg;
121 std::unique_ptr<clang::tooling::CompilationDatabase> compilations;
122 {
123 // loadFromCommandLine() may alter argc and argv, thus access fixed_argv
124 // through FixedArgvAccess.
125 FixedArgvAccess raw(fixed_argv);
126
127 compilations =
128 clang::tooling::FixedCompilationDatabase::loadFromCommandLine(
129 raw.argc_, raw.argv_, cmdline_error_msg);
130 }
131
132 // Parse the command line options
133 bool is_command_valid = llvm::cl::ParseCommandLineOptions(
134 fixed_argv.GetArgc(), fixed_argv.GetArgv(), "header-checker",
135 &llvm::errs());
136
137 if (print_resource_dir) {
138 bool ok = PrintResourceDir(fixed_argv.GetArgv()[0]);
139 ::exit(ok ? 0 : 1);
140 }
141
142 // Check required arguments after handling -print-resource-dir.
143 if (header_file.empty()) {
144 llvm::errs() << "ERROR: Expect exactly one positional argument\n";
145 is_command_valid = false;
146 } else if (!llvm::sys::fs::exists(header_file)) {
147 llvm::errs() << "ERROR: Source file \"" << header_file
148 << "\" is not found\n";
149 is_command_valid = false;
150 }
151
152 if (out_dump.empty()) {
153 llvm::errs() << "ERROR: Expect exactly one -o=<out_dump>\n";
154 is_command_valid = false;
155 }
156
157 // Print an error message if we failed to create the compilation database
158 // from the command line arguments. This check is intentionally performed
159 // after `llvm::cl::ParseCommandLineOptions()` so that `-help` can work
160 // without `--`.
161 if (!compilations) {
162 if (cmdline_error_msg.empty()) {
163 llvm::errs() << "ERROR: Failed to parse clang command line options\n";
164 } else {
165 llvm::errs() << "ERROR: " << cmdline_error_msg << "\n";
166 }
167 is_command_valid = false;
168 }
169
170 if (!is_command_valid) {
171 ::exit(1);
172 }
173
174 RootDirs parsed_root_dirs = ParseRootDirs(root_dirs);
175
176 bool dump_exported_only = (!no_filter && !exported_header_dirs.empty());
177 std::set<std::string> exported_headers =
178 CollectAllExportedHeaders(exported_header_dirs, parsed_root_dirs);
179
180 // Initialize clang tools and run front-end action.
181 std::vector<std::string> header_files{ header_file };
182 HeaderCheckerOptions options(NormalizePath(header_file, parsed_root_dirs),
183 out_dump, std::move(exported_headers),
184 std::move(parsed_root_dirs), output_format,
185 dump_exported_only, dump_function_declarations);
186
187 clang::tooling::ClangTool tool(*compilations, header_files);
188 std::unique_ptr<clang::tooling::FrontendActionFactory> factory(
189 new HeaderCheckerFrontendActionFactory(options));
190 return tool.run(factory.get());
191 }
192