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