1 // Copyright 2022 Google LLC 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 // https://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 #ifndef SANDBOXED_API_TOOLS_CLANG_GENERATOR_COMPILATION_DATABASE_H_ 16 #define SANDBOXED_API_TOOLS_CLANG_GENERATOR_COMPILATION_DATABASE_H_ 17 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include "clang/Tooling/ArgumentsAdjusters.h" 23 #include "clang/Tooling/CompilationDatabase.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/Error.h" 26 27 namespace sapi { 28 29 // Returns a CompilationDatabase that redirects to the specified inner database. 30 std::unique_ptr<clang::tooling::CompilationDatabase> NonOwningCompileCommands( 31 clang::tooling::CompilationDatabase& inner); 32 33 std::unique_ptr<clang::tooling::CompilationDatabase> 34 FromCxxAjustedCompileCommands( 35 std::unique_ptr<clang::tooling::CompilationDatabase> inner); 36 37 // A parser for options common to all command-line Clang tools. This class 38 // behaves the same as clang::tooling::CommonOptionsParser, except that it won't 39 // print an error if a compilation database could not be found. 40 class OptionsParser { 41 public: 42 static llvm::Expected<OptionsParser> create( 43 int& argc, const char** argv, llvm::cl::OptionCategory& category, 44 llvm::cl::NumOccurrencesFlag occurrences_flag = llvm::cl::OneOrMore, 45 const char* overview = nullptr); 46 getCompilations()47 clang::tooling::CompilationDatabase& getCompilations() { 48 return *compilations_; 49 } 50 getSourcePathList()51 const std::vector<std::string>& getSourcePathList() const { 52 return source_path_list_; 53 } 54 getArgumentsAdjuster()55 clang::tooling::ArgumentsAdjuster getArgumentsAdjuster() { return adjuster_; } 56 57 private: 58 OptionsParser() = default; 59 60 llvm::Error init(int& argc, const char** argv, 61 llvm::cl::OptionCategory& category, 62 llvm::cl::NumOccurrencesFlag occurrences_flag, 63 const char* overview); 64 65 std::unique_ptr<clang::tooling::CompilationDatabase> compilations_; 66 std::vector<std::string> source_path_list_; 67 clang::tooling::ArgumentsAdjuster adjuster_; 68 }; 69 70 } // namespace sapi 71 72 #endif // SANDBOXED_API_TOOLS_CLANG_GENERATOR_COMPILATION_DATABASE_H_ 73