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