llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Anutosh Bhat (anutosh491)

<details>
<summary>Changes</summary>

1) How usual clang works

It goes from Creating the Compiler Instance -&gt; [Addressing these 
llvmargs](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L231-L239)
  -&gt; 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 -&gt; calls 
executeAction (and doesn't handle some options in between)

So ExecuteAction is framed like this 
```
bool CompilerInstance::ExecuteAction(FrontendAction &amp;Act) {
  assert(hasDiagnostics() &amp;&amp; "Diagnostics engine is not initialized!");
  assert(!getFrontendOpts().ShowHelp &amp;&amp; "Client must handle '-help'!");
  assert(!getFrontendOpts().ShowVersion &amp;&amp; "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)










---
Full diff: https://github.com/llvm/llvm-project/pull/132670.diff


1 Files Affected:

- (modified) clang/lib/Interpreter/Interpreter.cpp (+37-1) 


``````````diff
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)

``````````

</details>


https://github.com/llvm/llvm-project/pull/132670
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to