================
@@ -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());
+}
----------------
Decodetalkers 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?
https://github.com/llvm/llvm-project/pull/204511
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits