Skip to content

Conversation

@Bigcheese
Copy link
Contributor

This can be used to trigger implicit module map lookup without also importing the module. This can be useful for debugging as it avoids loading the module map from the AST file, which has slightly different semantics.

This can be used to trigger implicit modulemap lookup without also
importing the module. This can be useful for debugging as it avoids
loading the module map from the AST file.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules labels Feb 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-modules

Author: Michael Spencer (Bigcheese)

Changes

This can be used to trigger implicit module map lookup without also importing the module. This can be useful for debugging as it avoids loading the module map from the AST file, which has slightly different semantics.


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+2)
  • (modified) clang/lib/Lex/Pragma.cpp (+17-1)
  • (added) clang/test/Modules/clang-pragmas.c (+22)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 2b1cc81677b08..0ae4f871288cb 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -715,6 +715,8 @@ def warn_pragma_debug_unexpected_command : Warning<
   "unexpected debug command '%0'">, InGroup<IgnoredPragmas>;
 def warn_pragma_debug_unknown_module : Warning<
   "unknown module '%0'">, InGroup<IgnoredPragmas>;
+def warn_pragma_debug_unable_to_find_module : Warning<
+  "unable to find module '%0'">, InGroup<IgnoredPragmas>;
 // #pragma module
 def err_pp_expected_module_name : Error<
   "expected %select{identifier after '.' in |}0module name">;
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index e339ca8422278..91c1619e35623 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -1119,11 +1119,27 @@ struct PragmaDebugHandler : public PragmaHandler {
         M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
         if (!M) {
           PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
-              << IIAndLoc.first;
+              << IIAndLoc.first->getName();
           return;
         }
       }
       M->dump();
+    } else if (II->isStr("module_lookup")) {
+      Token MName;
+      PP.LexUnexpandedToken(MName);
+      auto *MNameII = MName.getIdentifierInfo();
+      if (!MNameII) {
+        PP.Diag(MName, diag::warn_pragma_debug_missing_argument)
+            << II->getName();
+        return;
+      }
+      Module *M = PP.getHeaderSearchInfo().lookupModule(MNameII->getName());
+      if (!M) {
+        PP.Diag(MName, diag::warn_pragma_debug_unable_to_find_module)
+            << MNameII->getName();
+        return;
+      }
+      M->dump();
     } else if (II->isStr("overflow_stack")) {
       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
         DebugOverflowStack();
diff --git a/clang/test/Modules/clang-pragmas.c b/clang/test/Modules/clang-pragmas.c
new file mode 100644
index 0000000000000..22ead7279f84f
--- /dev/null
+++ b/clang/test/Modules/clang-pragmas.c
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -I%t %t/tu.c -fsyntax-only \
+// RUN:   -verify 2>&1 | FileCheck %s
+
+//--- module.modulemap
+
+module A {
+  header "A.h"
+}
+
+//--- A.h
+
+//--- tu.c
+
+#pragma clang __debug module_map A // expected-warning{{unknown module 'A'}}
+#pragma clang __debug module_lookup B // expected-warning{{unable to find module 'B'}}
+#pragma clang __debug module_lookup A // does header search for A
+#pragma clang __debug module_map A // now finds module A
+
+// CHECK: module A
+// CHECK: module A

@Bigcheese Bigcheese merged commit 1c4e0f6 into llvm:main Mar 3, 2025
15 checks passed
@Bigcheese Bigcheese deleted the dev/pragma_module_lookup branch March 12, 2025 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants