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/header_abi_util.h"
20
21 #include <clang/Frontend/FrontendActions.h>
22 #include <clang/Tooling/CommonOptionsParser.h>
23 #include <clang/Tooling/CompilationDatabase.h>
24 #include <clang/Tooling/Tooling.h>
25 #include <llvm/Support/CommandLine.h>
26 #include <llvm/Support/FileSystem.h>
27 #include <llvm/Support/raw_ostream.h>
28
29 #include <memory>
30 #include <string>
31 #include <vector>
32
33 #include <stdlib.h>
34
35
36 using header_checker::dumper::FixedArgv;
37 using header_checker::dumper::FixedArgvAccess;
38 using header_checker::dumper::FixedArgvRegistry;
39 using header_checker::dumper::HeaderCheckerFrontendActionFactory;
40 using header_checker::dumper::HeaderCheckerOptions;
41 using header_checker::repr::TextFormatIR;
42 using header_checker::utils::CollectAllExportedHeaders;
43 using header_checker::utils::RealPath;
44
45
46
47 static llvm::cl::OptionCategory header_checker_category(
48 "header-checker options");
49
50 static llvm::cl::opt<std::string> header_file(
51 llvm::cl::Positional, llvm::cl::desc("<source.cpp>"), llvm::cl::Required,
52 llvm::cl::cat(header_checker_category));
53
54 static llvm::cl::opt<std::string> out_dump(
55 "o", llvm::cl::value_desc("out_dump"), llvm::cl::Required,
56 llvm::cl::desc("Specify the reference dump file name"),
57 llvm::cl::cat(header_checker_category));
58
59 static llvm::cl::list<std::string> exported_header_dirs(
60 "I", llvm::cl::desc("<export_include_dirs>"), llvm::cl::Prefix,
61 llvm::cl::ZeroOrMore, llvm::cl::cat(header_checker_category));
62
63 static llvm::cl::opt<bool> no_filter(
64 "no-filter", llvm::cl::desc("Do not filter any abi"), llvm::cl::Optional,
65 llvm::cl::cat(header_checker_category));
66
67 static llvm::cl::opt<bool> suppress_errors(
68 "suppress-errors",
69 llvm::cl::desc("Suppress preprocess and semantic errors"),
70 llvm::cl::Optional, llvm::cl::cat(header_checker_category));
71
72 static llvm::cl::opt<bool> dump_function_declarations(
73 "dump-function-declarations",
74 llvm::cl::desc("Output the functions declared but not defined in the input "
75 "file"),
76 llvm::cl::Optional, llvm::cl::cat(header_checker_category));
77
78 static llvm::cl::opt<TextFormatIR> output_format(
79 "output-format", llvm::cl::desc("Specify format of output dump file"),
80 llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
81 "ProtobufTextFormat", "ProtobufTextFormat"),
82 clEnumValN(TextFormatIR::Json, "Json", "JSON")),
83 llvm::cl::init(TextFormatIR::Json),
84 llvm::cl::cat(header_checker_category));
85
86 // Hide irrelevant command line options defined in LLVM libraries.
HideIrrelevantCommandLineOptions()87 static void HideIrrelevantCommandLineOptions() {
88 llvm::StringMap<llvm::cl::Option *> &map = llvm::cl::getRegisteredOptions();
89 for (llvm::StringMapEntry<llvm::cl::Option *> &p : map) {
90 if (p.second->Category == &header_checker_category) {
91 continue;
92 }
93 if (p.first().startswith("help")) {
94 continue;
95 }
96 p.second->setHiddenFlag(llvm::cl::Hidden);
97 }
98 }
99
main(int argc,const char ** argv)100 int main(int argc, const char **argv) {
101 HideIrrelevantCommandLineOptions();
102
103 // Tweak argc and argv to workaround clang version mismatches.
104 FixedArgv fixed_argv(argc, argv);
105 FixedArgvRegistry::Apply(fixed_argv);
106
107 // Create compilation database from command line arguments after "--".
108 std::string cmdline_error_msg;
109 std::unique_ptr<clang::tooling::CompilationDatabase> compilations;
110 {
111 // loadFromCommandLine() may alter argc and argv, thus access fixed_argv
112 // through FixedArgvAccess.
113 FixedArgvAccess raw(fixed_argv);
114
115 compilations =
116 clang::tooling::FixedCompilationDatabase::loadFromCommandLine(
117 raw.argc_, raw.argv_, cmdline_error_msg);
118 }
119
120 // Parse the command line options
121 llvm::cl::ParseCommandLineOptions(
122 fixed_argv.GetArgc(), fixed_argv.GetArgv(), "header-checker");
123
124 // Print an error message if we failed to create the compilation database
125 // from the command line arguments. This check is intentionally performed
126 // after `llvm::cl::ParseCommandLineOptions()` so that `-help` can work
127 // without `--`.
128 if (!compilations) {
129 if (cmdline_error_msg.empty()) {
130 llvm::errs() << "ERROR: Failed to parse clang command line options\n";
131 } else {
132 llvm::errs() << "ERROR: " << cmdline_error_msg << "\n";
133 }
134 ::exit(1);
135 }
136
137 // Input header file existential check.
138 if (!llvm::sys::fs::exists(header_file)) {
139 llvm::errs() << "ERROR: Header file \"" << header_file << "\" not found\n";
140 ::exit(1);
141 }
142
143 std::set<std::string> exported_headers;
144 if (!no_filter) {
145 exported_headers = CollectAllExportedHeaders(exported_header_dirs);
146 }
147
148 // Initialize clang tools and run front-end action.
149 std::vector<std::string> header_files{ header_file };
150 HeaderCheckerOptions options(RealPath(header_file), out_dump,
151 std::move(exported_headers), output_format,
152 dump_function_declarations, suppress_errors);
153
154 clang::tooling::ClangTool tool(*compilations, header_files);
155 std::unique_ptr<clang::tooling::FrontendActionFactory> factory(
156 new HeaderCheckerFrontendActionFactory(options));
157 return tool.run(factory.get());
158 }
159