Skip to content

Commit 0097f64

Browse files
authored
Add --cxxflags option to cpp-rule-preprocessor (#188)
Adds a command-line option for passing additional flags to `cpp-rule-preprocessor`. This enables parsing files that require specific compilation options, such as `-std=c++23`.
1 parent 8835b45 commit 0097f64

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

cpp2rust/cpp_rule_preprocessor.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <clang/Sema/Template.h>
1717
#include <clang/Tooling/CompilationDatabase.h>
1818
#include <clang/Tooling/Tooling.h>
19+
#include <llvm/ADT/ArrayRef.h>
20+
#include <llvm/ADT/SmallVector.h>
21+
#include <llvm/ADT/StringRef.h>
1922
#include <llvm/Support/CommandLine.h>
2023
#include <llvm/Support/FormatVariadic.h>
2124
#include <llvm/Support/JSON.h>
@@ -760,8 +763,10 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
760763
Callback cb_;
761764
};
762765

763-
void Extract(const std::filesystem::path &src_path, llvm::json::Object &out) {
766+
void Extract(const std::filesystem::path &src_path, llvm::json::Object &out,
767+
llvm::ArrayRef<llvm::StringRef> cxx_flags) {
764768
auto flags = getPlatformClangBeginFlags();
769+
flags.insert(flags.end(), cxx_flags.begin(), cxx_flags.end());
765770
auto end_flags = getPlatformClangEndFlags();
766771
flags.insert(flags.end(), end_flags.begin(), end_flags.end());
767772
clang::tooling::FixedCompilationDatabase compilations(".", flags);
@@ -788,12 +793,19 @@ llvm::cl::opt<std::string>
788793
llvm::cl::value_desc("out.json"), llvm::cl::Required,
789794
llvm::cl::cat(cat));
790795

796+
llvm::cl::list<std::string> CXXFlags("cxxflags",
797+
llvm::cl::desc("Additional CXXFLAGS"),
798+
llvm::cl::value_desc("cxxflags"),
799+
llvm::cl::ZeroOrMore, llvm::cl::cat(cat));
800+
791801
} // namespace
792802

793803
int main(int argc, char *argv[]) {
794804
llvm::cl::HideUnrelatedOptions(cat);
795805
llvm::cl::ParseCommandLineOptions(argc, argv);
796806

807+
llvm::SmallVector<llvm::StringRef, 4> cxx_flags(CXXFlags.begin(),
808+
CXXFlags.end());
797809
fs::path dir = SrcDir.getValue();
798810
llvm::json::Object root;
799811
for (const char *name : {"src.c", "src.cpp"}) {
@@ -803,7 +815,7 @@ int main(int argc, char *argv[]) {
803815
}
804816
llvm::errs() << "Preprocessing " << path.string() << '\n';
805817
llvm::json::Object file_root;
806-
cpp2rust::Extract(path, file_root);
818+
cpp2rust::Extract(path, file_root, cxx_flags);
807819
for (auto &[k, v] : file_root) {
808820
if (!root.try_emplace(k, std::move(v)).second) {
809821
llvm::errs() << "ERROR: rule name " << k.str()

0 commit comments

Comments
 (0)