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