Skip to content

Conversation

jansvoboda11
Copy link
Contributor

@jansvoboda11 jansvoboda11 commented Sep 15, 2025

This PR changes the behavior of clang::ExecuteCompilerInvocation() so that it only returns early when the DiagnosticsEngine emitted errors within the function. Handling errors emitted before the function got called is a responsibility of the caller. Necessary for #158381.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Sep 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2025

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

This PR changes the behavior of clang::ExecuteCompilerInvocation() so that it doesn't return early whenever the DiagnosticsEngine emitted errors before the function got called. Handling that situation is the responsibility of the caller. Necessary for #158381.


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

1 Files Affected:

  • (modified) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp (+3-1)
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 9a6844d5f7d40..f1965a4e27e07 100644
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -210,6 +210,8 @@ CreateFrontendAction(CompilerInstance &CI) {
 }
 
 bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
+  unsigned NumErrorsBefore = Clang->getDiagnostics().getNumErrors();
+
   // Honor -help.
   if (Clang->getFrontendOpts().ShowHelp) {
     driver::getDriverOptTable().printHelp(
@@ -293,7 +295,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
 #endif
 
   // If there were errors in processing arguments, don't do anything else.
-  if (Clang->getDiagnostics().hasErrorOccurred())
+  if (Clang->getDiagnostics().getNumErrors() != NumErrorsBefore)
     return false;
   // Create and execute the frontend action.
   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));

Copy link
Collaborator

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more comment to explain when do we actually call this function when Diagnostics Engine already has error? This will help to evaluate what is a right thing to do.

@jansvoboda11
Copy link
Contributor Author

Currently we don't, but with #158381 we'll try to initialize the VFS overlays before calling clang::ExecuteCompilerInvocation(). If that VFS initialization fails, we still want to run the frontend action as per clang/test/VFS/module_missing_vfs.m and 005c2e5. Blindly checking hasErrorOccurred() without scope stops the compilation very soon instead of continuing. Note that right now, we initialize VFS overlays deep within this function under ExecuteAction(), so the hasErrorOccurred() logic doesn't affect it.

Copy link
Collaborator

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you summarize the reason and leave a comment in the code?

@jansvoboda11 jansvoboda11 changed the title [clang] Don't fail clang::ExecuteCompilerInvocation() for unrelated errors [clang] Don't fail clang::ExecuteCompilerInvocation() for caller errors Sep 15, 2025
@jansvoboda11 jansvoboda11 changed the title [clang] Don't fail clang::ExecuteCompilerInvocation() for caller errors [clang] Don't fail ExecuteCompilerInvocation() due to caller errors Sep 15, 2025
@jansvoboda11 jansvoboda11 merged commit f33fb0d into llvm:main Sep 15, 2025
9 checks passed
@jansvoboda11 jansvoboda11 deleted the execute-error-handling branch September 15, 2025 21:30
kimsh02 pushed a commit to kimsh02/llvm-project that referenced this pull request Sep 19, 2025
…llvm#158695)

This PR changes the behavior of `clang::ExecuteCompilerInvocation()` so
that it only returns early when the `DiagnosticsEngine` emitted errors
**within** the function. Handling errors emitted before the function got
called is a responsibility of the caller. Necessary for llvm#158381.
itzexpoexpo pushed a commit to itzexpoexpo/llvm-project that referenced this pull request Sep 21, 2025
…llvm#158695)

This PR changes the behavior of `clang::ExecuteCompilerInvocation()` so
that it only returns early when the `DiagnosticsEngine` emitted errors
**within** the function. Handling errors emitted before the function got
called is a responsibility of the caller. Necessary for llvm#158381.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants