Skip to content

Commit 1c4e0f6

Browse files
authored
[clang] Add #pragma clang __debug module_lookup (#129158)
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.
1 parent eee3db5 commit 1c4e0f6

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ def warn_pragma_debug_unexpected_command : Warning<
718718
"unexpected debug command '%0'">, InGroup<IgnoredPragmas>;
719719
def warn_pragma_debug_unknown_module : Warning<
720720
"unknown module '%0'">, InGroup<IgnoredPragmas>;
721+
def warn_pragma_debug_unable_to_find_module : Warning<
722+
"unable to find module '%0'">, InGroup<IgnoredPragmas>;
721723
// #pragma module
722724
def err_pp_expected_module_name : Error<
723725
"expected %select{identifier after '.' in |}0module name">;

clang/lib/Lex/Pragma.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,27 @@ struct PragmaDebugHandler : public PragmaHandler {
11191119
M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
11201120
if (!M) {
11211121
PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1122-
<< IIAndLoc.first;
1122+
<< IIAndLoc.first->getName();
11231123
return;
11241124
}
11251125
}
11261126
M->dump();
1127+
} else if (II->isStr("module_lookup")) {
1128+
Token MName;
1129+
PP.LexUnexpandedToken(MName);
1130+
auto *MNameII = MName.getIdentifierInfo();
1131+
if (!MNameII) {
1132+
PP.Diag(MName, diag::warn_pragma_debug_missing_argument)
1133+
<< II->getName();
1134+
return;
1135+
}
1136+
Module *M = PP.getHeaderSearchInfo().lookupModule(MNameII->getName());
1137+
if (!M) {
1138+
PP.Diag(MName, diag::warn_pragma_debug_unable_to_find_module)
1139+
<< MNameII->getName();
1140+
return;
1141+
}
1142+
M->dump();
11271143
} else if (II->isStr("overflow_stack")) {
11281144
if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
11291145
DebugOverflowStack();

clang/test/Modules/clang-pragmas.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -I%t %t/tu.c -fsyntax-only \
4+
// RUN: -verify 2>&1 | FileCheck %s
5+
6+
//--- module.modulemap
7+
8+
module A {
9+
header "A.h"
10+
}
11+
12+
//--- A.h
13+
14+
//--- tu.c
15+
16+
#pragma clang __debug module_map A // expected-warning{{unknown module 'A'}}
17+
#pragma clang __debug module_lookup B // expected-warning{{unable to find module 'B'}}
18+
#pragma clang __debug module_lookup A // does header search for A
19+
#pragma clang __debug module_map A // now finds module A
20+
21+
// CHECK: module A
22+
// CHECK: module A

0 commit comments

Comments
 (0)