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 "fixed_argv.h"
16 #include "frontend_action_factory.h"
17 
18 #include <header_abi_util.h>
19 
20 #include <clang/Frontend/FrontendActions.h>
21 #include <clang/Tooling/CommonOptionsParser.h>
22 #include <clang/Tooling/CompilationDatabase.h>
23 #include <clang/Tooling/Tooling.h>
24 #include <llvm/Support/CommandLine.h>
25 #include <llvm/Support/FileSystem.h>
26 #include <llvm/Support/raw_ostream.h>
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <stdlib.h>
33 
34 static llvm::cl::OptionCategory header_checker_category(
35     "header-checker options");
36 
37 static llvm::cl::opt<std::string> header_file(
38     llvm::cl::Positional, llvm::cl::desc("<source.cpp>"), llvm::cl::Required,
39     llvm::cl::cat(header_checker_category));
40 
41 static llvm::cl::opt<std::string> out_dump(
42     "o", llvm::cl::value_desc("out_dump"), llvm::cl::Required,
43     llvm::cl::desc("Specify the reference dump file name"),
44     llvm::cl::cat(header_checker_category));
45 
46 static llvm::cl::list<std::string> exported_header_dirs(
47     "I", llvm::cl::desc("<export_include_dirs>"), llvm::cl::Prefix,
48     llvm::cl::ZeroOrMore, llvm::cl::cat(header_checker_category));
49 
50 static llvm::cl::opt<bool> no_filter(
51     "no-filter", llvm::cl::desc("Do not filter any abi"), llvm::cl::Optional,
52     llvm::cl::cat(header_checker_category));
53 
54 static llvm::cl::opt<abi_util::TextFormatIR> text_format(
55     "text-format", llvm::cl::desc("Specify text format of abi dump"),
56     llvm::cl::values(clEnumValN(abi_util::TextFormatIR::ProtobufTextFormat,
57                                 "ProtobufTextFormat", "ProtobufTextFormat"),
58                      clEnumValEnd),
59     llvm::cl::init(abi_util::TextFormatIR::ProtobufTextFormat),
60     llvm::cl::cat(header_checker_category));
61 
62 // Hide irrelevant command line options defined in LLVM libraries.
HideIrrelevantCommandLineOptions()63 static void HideIrrelevantCommandLineOptions() {
64   llvm::StringMap<llvm::cl::Option *> &map = llvm::cl::getRegisteredOptions();
65   for (llvm::StringMapEntry<llvm::cl::Option *> &p : map) {
66     if (p.second->Category == &header_checker_category) {
67       continue;
68     }
69     if (p.first().startswith("help")) {
70       continue;
71     }
72     p.second->setHiddenFlag(llvm::cl::Hidden);
73   }
74 }
75 
76 
main(int argc,const char ** argv)77 int main(int argc, const char **argv) {
78   HideIrrelevantCommandLineOptions();
79 
80   // Tweak argc and argv to workaround clang version mismatches.
81   FixedArgv fixed_argv(argc, argv);
82   FixedArgvRegistry::Apply(fixed_argv);
83 
84   // Create compilation database from command line arguments after "--".
85   std::unique_ptr<clang::tooling::CompilationDatabase> compilations;
86 
87   {
88     // loadFromCommandLine() may alter argc and argv, thus access fixed_argv
89     // through FixedArgvAccess.
90     FixedArgvAccess raw(fixed_argv);
91     compilations.reset(
92         clang::tooling::FixedCompilationDatabase::loadFromCommandLine(
93             raw.argc_, raw.argv_));
94   }
95 
96   // Parse the command line options.
97   llvm::cl::ParseCommandLineOptions(
98       fixed_argv.GetArgc(), fixed_argv.GetArgv(), "header-checker");
99 
100   // Input header file existential check.
101   if (!llvm::sys::fs::exists(header_file)) {
102     llvm::errs() << "ERROR: Header file \"" << header_file << "\" not found\n";
103     ::exit(1);
104   }
105 
106   // Check whether we can create compilation database and deduce compiler
107   // options from command line options.
108   if (!compilations) {
109     llvm::errs() << "ERROR: Clang compilation options not specified.\n";
110     ::exit(1);
111   }
112 
113   std::set<std::string> exported_headers;
114   if (!no_filter) {
115     exported_headers =
116         abi_util::CollectAllExportedHeaders(exported_header_dirs);
117   }
118 
119   // Initialize clang tools and run front-end action.
120   std::vector<std::string> header_files{ header_file };
121 
122   clang::tooling::ClangTool tool(*compilations, header_files);
123   std::unique_ptr<clang::tooling::FrontendActionFactory> factory(
124       new HeaderCheckerFrontendActionFactory(out_dump, exported_headers,
125                                              text_format));
126 
127   return tool.run(factory.get());
128 }
129