aboutsummaryrefslogtreecommitdiff
path: root/clang
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2023-08-21 18:57:46 +0200
committerSam McCall <sam.mccall@gmail.com>2023-08-22 11:55:51 +0200
commit23459f13fcd9c424a41debb2d11eb56843d4e679 (patch)
tree9cc00624f01f0554818009b4d82a4f0ec270531e /clang
parent5f6c03636d22029550c8ab94388827e728a0c5fe (diff)
downloadllvm-23459f13fcd9c424a41debb2d11eb56843d4e679.zip
llvm-23459f13fcd9c424a41debb2d11eb56843d4e679.tar.gz
llvm-23459f13fcd9c424a41debb2d11eb56843d4e679.tar.bz2
[Lex] Preambles should contain the global module fragment.
For applications like clangd, the preamble remains an important optimization when editing a module definition. The global module fragment is a good fit for it as it by definition contains only preprocessor directives. Before this patch, we would terminate the preamble immediately at the "module" keyword. Differential Revision: https://reviews.llvm.org/D158439
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Lex/Lexer.cpp16
-rw-r--r--clang/unittests/Lex/CMakeLists.txt1
-rw-r--r--clang/unittests/Lex/LexerTest.cpp34
3 files changed, 51 insertions, 0 deletions
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index f3c0e80..f2b8818 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -705,6 +705,22 @@ PreambleBounds Lexer::ComputePreamble(StringRef Buffer,
// directive or it was one that can't occur in the preamble at this
// point. Roll back the current token to the location of the '#'.
TheTok = HashTok;
+ } else if (TheTok.isAtStartOfLine() &&
+ TheTok.getKind() == tok::raw_identifier &&
+ TheTok.getRawIdentifier() == "module" &&
+ LangOpts.CPlusPlusModules) {
+ // The initial global module fragment introducer "module;" is part of
+ // the preamble, which runs up to the module declaration "module foo;".
+ Token ModuleTok = TheTok;
+ do {
+ TheLexer.LexFromRawLexer(TheTok);
+ } while (TheTok.getKind() == tok::comment);
+ if (TheTok.getKind() != tok::semi) {
+ // Not global module fragment, roll back.
+ TheTok = ModuleTok;
+ break;
+ }
+ continue;
}
// We hit a token that we don't recognize as being in the
diff --git a/clang/unittests/Lex/CMakeLists.txt b/clang/unittests/Lex/CMakeLists.txt
index aead76c..f7fc0ee 100644
--- a/clang/unittests/Lex/CMakeLists.txt
+++ b/clang/unittests/Lex/CMakeLists.txt
@@ -25,5 +25,6 @@ clang_target_link_libraries(LexTests
target_link_libraries(LexTests
PRIVATE
+ LLVMTestingAnnotations
LLVMTestingSupport
)
diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp
index 44ae368..8932265 100644
--- a/clang/unittests/Lex/LexerTest.cpp
+++ b/clang/unittests/Lex/LexerTest.cpp
@@ -26,9 +26,11 @@
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Testing/Annotations/Annotations.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>
+#include <string>
#include <vector>
namespace {
@@ -660,4 +662,36 @@ TEST_F(LexerTest, RawAndNormalLexSameForLineComments) {
}
EXPECT_TRUE(ToksView.empty());
}
+
+TEST(LexerPreambleTest, PreambleBounds) {
+ std::vector<std::string> Cases = {
+ R"cc([[
+ #include <foo>
+ ]]int bar;
+ )cc",
+ R"cc([[
+ #include <foo>
+ ]])cc",
+ R"cc([[
+ // leading comment
+ #include <foo>
+ ]]// trailing comment
+ int bar;
+ )cc",
+ R"cc([[
+ module;
+ #include <foo>
+ ]]module bar;
+ int x;
+ )cc",
+ };
+ for (const auto& Case : Cases) {
+ llvm::Annotations A(Case);
+ clang::LangOptions LangOpts;
+ LangOpts.CPlusPlusModules = true;
+ auto Bounds = Lexer::ComputePreamble(A.code(), LangOpts);
+ EXPECT_EQ(Bounds.Size, A.range().End) << Case;
+ }
+}
+
} // anonymous namespace