• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::GetCwd;
46 using header_checker::utils::HideIrrelevantCommandLineOptions;
47 using header_checker::utils::NormalizePath;
48 
49 
50 static llvm::cl::OptionCategory header_checker_category(
51     "header-checker options");
52 
53 static llvm::cl::opt<std::string> header_file(
54     llvm::cl::Positional, llvm::cl::desc("<source.cpp>"), llvm::cl::Optional,
55     llvm::cl::cat(header_checker_category));
56 
57 static llvm::cl::opt<std::string> out_dump(
58     "o", llvm::cl::value_desc("out_dump"), llvm::cl::Optional,
59     llvm::cl::desc("Specify the reference dump file name"),
60     llvm::cl::cat(header_checker_category));
61 
62 static llvm::cl::list<std::string> exported_header_dirs(
63     "I", llvm::cl::desc("<export_include_dirs>"), llvm::cl::Prefix,
64     llvm::cl::ZeroOrMore, llvm::cl::cat(header_checker_category));
65 
66 static llvm::cl::opt<std::string> root_dir(
67     "root-dir",
68     llvm::cl::desc("Specify the directory that the paths in the dump file are "
69                    "relative to. Default to current working directory"),
70     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
71 
72 static llvm::cl::opt<bool> no_filter(
73     "no-filter", llvm::cl::desc("Do not filter any abi"), llvm::cl::Optional,
74     llvm::cl::cat(header_checker_category));
75 
76 static llvm::cl::opt<bool> suppress_errors(
77     "suppress-errors",
78     llvm::cl::desc("Suppress preprocess and semantic errors"),
79     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
80 
81 static llvm::cl::opt<bool> dump_function_declarations(
82     "dump-function-declarations",
83     llvm::cl::desc("Output the functions declared but not defined in the input "
84                    "file"),
85     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
86 
87 static llvm::cl::opt<TextFormatIR> output_format(
88     "output-format", llvm::cl::desc("Specify format of output dump file"),
89     llvm::cl::values(clEnumValN(TextFormatIR::ProtobufTextFormat,
90                                 "ProtobufTextFormat", "ProtobufTextFormat"),
91                      clEnumValN(TextFormatIR::Json, "Json", "JSON")),
92     llvm::cl::init(TextFormatIR::Json),
93     llvm::cl::cat(header_checker_category));
94 
95 static llvm::cl::opt<bool> print_resource_dir(
96     "print-resource-dir",
97     llvm::cl::desc("Print real path to default resource directory"),
98     llvm::cl::Optional, llvm::cl::cat(header_checker_category));
99 
100 int main(int argc, const char **argv);
101 
PrintResourceDir(const char * argv_0)102 static bool PrintResourceDir(const char *argv_0) {
103   std::string program_path =
104       llvm::sys::fs::getMainExecutable(argv_0, (void *)main);
105   if (program_path.empty()) {
106     llvm::errs() << "Failed to get program path\n";
107     return false;
108   }
109   llvm::outs() << clang::driver::Driver::GetResourcesPath(program_path) << "\n";
110   return true;
111 }
112 
main(int argc,const char ** argv)113 int main(int argc, const char **argv) {
114   HideIrrelevantCommandLineOptions(header_checker_category);
115 
116   // Tweak argc and argv to workaround clang version mismatches.
117   FixedArgv fixed_argv(argc, argv);
118   FixedArgvRegistry::Apply(fixed_argv);
119 
120   // Create compilation database from command line arguments after "--".
121   std::string cmdline_error_msg;
122   std::unique_ptr<clang::tooling::CompilationDatabase> compilations;
123   {
124     // loadFromCommandLine() may alter argc and argv, thus access fixed_argv
125     // through FixedArgvAccess.
126     FixedArgvAccess raw(fixed_argv);
127 
128     compilations =
129         clang::tooling::FixedCompilationDatabase::loadFromCommandLine(
130             raw.argc_, raw.argv_, cmdline_error_msg);
131   }
132 
133   // Parse the command line options
134   bool is_command_valid = llvm::cl::ParseCommandLineOptions(
135       fixed_argv.GetArgc(), fixed_argv.GetArgv(), "header-checker",
136       &llvm::errs());
137 
138   if (print_resource_dir) {
139     bool ok = PrintResourceDir(fixed_argv.GetArgv()[0]);
140     ::exit(ok ? 0 : 1);
141   }
142 
143   // Check required arguments after handling -print-resource-dir.
144   if (header_file.empty()) {
145     llvm::errs() << "ERROR: Expect exactly one positional argument\n";
146     is_command_valid = false;
147   } else if (!llvm::sys::fs::exists(header_file)) {
148     llvm::errs() << "ERROR: Source file \"" << header_file
149                  << "\" is not found\n";
150     is_command_valid = false;
151   }
152 
153   if (out_dump.empty()) {
154     llvm::errs() << "ERROR: Expect exactly one -o=<out_dump>\n";
155     is_command_valid = false;
156   }
157 
158   // Print an error message if we failed to create the compilation database
159   // from the command line arguments. This check is intentionally performed
160   // after `llvm::cl::ParseCommandLineOptions()` so that `-help` can work
161   // without `--`.
162   if (!compilations) {
163     if (cmdline_error_msg.empty()) {
164       llvm::errs() << "ERROR: Failed to parse clang command line options\n";
165     } else {
166       llvm::errs() << "ERROR: " << cmdline_error_msg << "\n";
167     }
168     is_command_valid = false;
169   }
170 
171   if (!is_command_valid) {
172     ::exit(1);
173   }
174 
175   const std::string root_dir_or_cwd = (root_dir.empty() ? GetCwd() : root_dir);
176 
177   bool dump_exported_only = (!no_filter && !exported_header_dirs.empty());
178   std::set<std::string> exported_headers =
179       CollectAllExportedHeaders(exported_header_dirs, root_dir_or_cwd);
180 
181   // Initialize clang tools and run front-end action.
182   std::vector<std::string> header_files{ header_file };
183   HeaderCheckerOptions options(
184       NormalizePath(header_file, root_dir_or_cwd), out_dump,
185       std::move(exported_headers), root_dir_or_cwd, output_format,
186       dump_exported_only, dump_function_declarations, suppress_errors);
187 
188   clang::tooling::ClangTool tool(*compilations, header_files);
189   std::unique_ptr<clang::tooling::FrontendActionFactory> factory(
190       new HeaderCheckerFrontendActionFactory(options));
191   return tool.run(factory.get());
192 }
193