https://github.com/anutosh491 created 
https://github.com/llvm/llvm-project/pull/132670

1) How usual clang works

It goes from Creating the Compiler Instance -> [Addressing these 
llvmargs](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L231-L239)
  -> calling 
[executeAction](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L280)

2) what clang-repl does is create the Compiler Instance -> calls executeAction 
(and doesn't handle some options in between)

So ExecuteAction is framed like this 
```
bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
....
```

And clang-repl would need to handle these before getting to executeAction.

Otherwise 

1) processing the -Xclang -version flag would give us the following (as shown 
while running clang-repl in the browser)

![image](https://github.com/user-attachments/assets/68e42810-4288-4d88-af9a-de8a95a0be2d)

2) This would also help us process a try-catch block while running clang-repl 
in the browser which can't be done currently 

![image](https://github.com/user-attachments/assets/c928d07e-e83c-46e1-8e48-39fe5e34a874)










>From 6545414a97b6458333f399c7252ae55c88a42d62 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat...@gmail.com>
Date: Mon, 24 Mar 2025 10:09:59 +0530
Subject: [PATCH] Handle frontend options for clang-repl before calling
 executeAction

---
 clang/lib/Interpreter/Interpreter.cpp | 38 ++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index fa4c1439c9261..5f48117dbf3b8 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -141,6 +141,37 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
   return std::move(Clang);
 }
 
+static llvm::Error HandleFrontendOptions(const CompilerInstance &CI) {
+  const auto &FrontendOpts = CI.getFrontendOpts();
+
+  if (FrontendOpts.ShowHelp) {
+    driver::getDriverOptTable().printHelp(
+        llvm::outs(), "clang -cc1 [options] file...",
+        "LLVM 'Clang' Compiler: http://clang.llvm.org";,
+        /*ShowHidden=*/false, /*ShowAllAliases=*/false,
+        llvm::opt::Visibility(driver::options::CC1Option));
+    return llvm::createStringError(llvm::errc::not_supported, "Help 
displayed");
+  }
+
+  if (FrontendOpts.ShowVersion) {
+    llvm::cl::PrintVersionMessage();
+    return llvm::createStringError(llvm::errc::not_supported, "Version 
displayed");
+  }
+
+  if (!FrontendOpts.LLVMArgs.empty()) {
+    unsigned NumArgs = FrontendOpts.LLVMArgs.size();
+    auto Args = std::make_unique<const char*[]>(NumArgs + 2);
+    Args[0] = "clang-repl (LLVM option parsing)";
+    for (unsigned i = 0; i != NumArgs; ++i)
+      Args[i + 1] = FrontendOpts.LLVMArgs[i].c_str();
+    Args[NumArgs + 1] = nullptr;
+    llvm::errs() << "Parsing LLVM backend options via 
cl::ParseCommandLineOptions...\n";
+    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
+  }
+
+  return llvm::Error::success();
+}
+
 } // anonymous namespace
 
 namespace clang {
@@ -451,7 +482,12 @@ const char *const Runtimes = R"(
 
 llvm::Expected<std::unique_ptr<Interpreter>>
 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
-  llvm::Error Err = llvm::Error::success();
+
+  llvm::Error Err = HandleFrontendOptions(*CI);
+  if (Err) {
+    return std::move(Err);
+  }
+
   auto Interp =
       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
   if (Err)

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to