Skip to content

Commit dd3e6c8

Browse files
authored
Support C++20 Modules in clang-repl (#79261)
This comes from when I playing around clang-repl with moduels : ) I succeeded to import std with https://libcxx.llvm.org/Modules.html and calling `std::printf` after this patch. I want to put the documentation part to https://clang.llvm.org/docs/StandardCPlusPlusModules.html in a separate commit.
1 parent 78b00c1 commit dd3e6c8

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Crash and bug fixes
191191
Improvements
192192
^^^^^^^^^^^^
193193

194+
- Support importing C++20 modules in clang-repl.
195+
194196
Moved checkers
195197
^^^^^^^^^^^^^^
196198

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ VALUE_LANGOPT(FuchsiaAPILevel, 32, 0, "Fuchsia API level")
485485
// on large _BitInts.
486486
BENIGN_VALUE_LANGOPT(MaxBitIntWidth, 32, 128, "Maximum width of a _BitInt")
487487

488-
LANGOPT(IncrementalExtensions, 1, 0, " True if we want to process statements"
488+
COMPATIBLE_LANGOPT(IncrementalExtensions, 1, 0, " True if we want to process statements"
489489
"on the global scope, ignore EOF token and continue later on (thus "
490490
"avoid tearing the Lexer and etc. down). Controlled by "
491491
"-fincremental-extensions.")

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,10 +3286,10 @@ DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
32863286
if (auto *OID = dyn_cast<ObjCInterfaceDecl>(DC))
32873287
return OID->getDefinition();
32883288

3289-
// We can see the TU here only if we have no Sema object. In that case,
3290-
// there's no TU scope to look in, so using the DC alone is sufficient.
3289+
// We can see the TU here only if we have no Sema object. It is possible
3290+
// we're in clang-repl so we still need to get the primary context.
32913291
if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3292-
return TU;
3292+
return TU->getPrimaryContext();
32933293

32943294
return nullptr;
32953295
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// UNSUPPORTED: system-aix
2+
//
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: split-file %s %t
6+
//
7+
// RUN: %clang -std=c++20 %t/mod.cppm --precompile \
8+
// RUN: -o %t/mod.pcm
9+
// RUN: %clang %t/mod.pcm -c -o %t/mod.o
10+
// RUN: %clang -shared %t/mod.o -o %t/libmod.so
11+
//
12+
// RUN: cat %t/import.cpp | env LD_LIBRARY_PATH=%t:$LD_LIBRARY_PATH \
13+
// RUN: clang-repl -Xcc=-std=c++20 -Xcc=-fmodule-file=M=%t/mod.pcm \
14+
// RUN: | FileCheck %t/import.cpp
15+
16+
//--- mod.cppm
17+
export module M;
18+
export const char* Hello() {
19+
return "Hello Interpreter for Modules!";
20+
}
21+
22+
//--- import.cpp
23+
24+
%lib libmod.so
25+
26+
import M;
27+
28+
extern "C" int printf(const char *, ...);
29+
printf("%s\n", Hello());
30+
31+
// CHECK: Hello Interpreter for Modules!

0 commit comments

Comments
 (0)