================
@@ -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:

> how to do it... Emm, I do not quite understand this part, so I copied them.. 
> annote is used to check if the code addressed by highlight can back to 
> original code.. I think that is needed... do you mean I just need to check if 
> the highlight in the right place is with the right token?

I mean that we only need to check that `getSemanticHighlightings()` return 
value contains tokens for import/export with correct kind, we don't need to 
check the full highlighting of the code.

I.e. in your test after `auto Actual =...` you can add something like this:
```cpp
  auto HasToken = [&](llvm::StringRef Name, HighlightingKind Kind) {
    return std::any_of(Actual.begin(), Actual.end(),
                       [&](const HighlightingToken &T) {
                         return T.Kind == Kind && T.R == UseCpp.range(Name);
                       });
  };
  EXPECT_TRUE(HasToken("import", HighlightingKind::Modifier));
  EXPECT_TRUE(HasToken("export0", HighlightingKind::Modifier));
  EXPECT_TRUE(HasToken("export1", HighlightingKind::Modifier));
```

https://github.com/llvm/llvm-project/pull/204511
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to