[clang-tools-extra] feat: highlight for import, export keyword of CXX Module (PR #204511)

Aleksandr Platonov via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 5 23:15:38 PDT 2026


================
@@ -282,6 +283,50 @@ class PrerequisiteModulesTests : public ::testing::Test {
   DiagnosticConsumer DiagConsumer;
 };
 
+/// Annotates the input code with provided semantic highlightings. Results look
+/// something like:
+///   class $Class[[X]] {
+///     $Primitive[[int]] $Field[[a]] = 0;
+///   };
+std::string annotate(llvm::StringRef Input,
+                     llvm::ArrayRef<HighlightingToken> Tokens) {
+  assert(llvm::is_sorted(
+      Tokens, [](const HighlightingToken &L, const HighlightingToken &R) {
+        return L.R.start < R.R.start;
+      }));
+
+  std::string Buf;
+  llvm::raw_string_ostream OS(Buf);
+  unsigned NextChar = 0;
+  for (auto &T : Tokens) {
+    unsigned StartOffset = llvm::cantFail(positionToOffset(Input, T.R.start));
+    unsigned EndOffset = llvm::cantFail(positionToOffset(Input, T.R.end));
+    assert(StartOffset <= EndOffset);
+    assert(NextChar <= StartOffset);
+
+    bool hasDef =
+        T.Modifiers & (1 << uint32_t(HighlightingModifier::Definition));
+    bool hasDecl =
+        T.Modifiers & (1 << uint32_t(HighlightingModifier::Declaration));
+    EXPECT_TRUE(!hasDef || hasDecl);
+
+    OS << Input.substr(NextChar, StartOffset - NextChar);
+    OS << '$' << T.Kind;
+    for (unsigned I = 0;
+         I <= static_cast<uint32_t>(HighlightingModifier::LastModifier); ++I) {
+      if (T.Modifiers & (1 << I)) {
+        // _decl_def is common and redundant, just print _def instead.
+        if (I != uint32_t(HighlightingModifier::Declaration) || !hasDef)
+          OS << '_' << static_cast<HighlightingModifier>(I);
+      }
+    }
+    OS << "[[" << Input.substr(StartOffset, EndOffset - StartOffset) << "]]";
+    NextChar = EndOffset;
+  }
+  OS << Input.substr(NextChar);
+  return std::move(OS.str());
+}
----------------
ArcsinX wrote:

I don't think there's any point in copying this from SemanticHighlightingTests.cpp.
We don't really need full validation of all the highlighting. In this specific case, it seems sufficient to simply verify that the `import` and `export` tokens have the correct kind, while the rest of the highlighting is covered by the semantic highlighting tests.

https://github.com/llvm/llvm-project/pull/204511


More information about the cfe-commits mailing list