| 1 | //==- SemanticHighlightingTests.cpp - SemanticHighlighting tests-*- C++ -* -==// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Annotations.h" |
| 10 | #include "Protocol.h" |
| 11 | #include "SemanticHighlighting.h" |
| 12 | #include "SourceCode.h" |
| 13 | #include "TestTU.h" |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include "llvm/Support/ScopedPrinter.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include "gmock/gmock.h" |
| 21 | #include <algorithm> |
| 22 | |
| 23 | namespace clang { |
| 24 | namespace clangd { |
| 25 | namespace { |
| 26 | |
| 27 | using testing::IsEmpty; |
| 28 | using testing::SizeIs; |
| 29 | |
| 30 | /// Annotates the input code with provided semantic highlightings. Results look |
| 31 | /// something like: |
| 32 | /// class $Class[[X]] { |
| 33 | /// $Primitive[[int]] $Field[[a]] = 0; |
| 34 | /// }; |
| 35 | std::string annotate(llvm::StringRef Input, |
| 36 | llvm::ArrayRef<HighlightingToken> Tokens) { |
| 37 | assert(llvm::is_sorted( |
| 38 | Tokens, [](const HighlightingToken &L, const HighlightingToken &R) { |
| 39 | return L.R.start < R.R.start; |
| 40 | })); |
| 41 | |
| 42 | std::string Buf; |
| 43 | llvm::raw_string_ostream OS(Buf); |
| 44 | unsigned NextChar = 0; |
| 45 | for (auto &T : Tokens) { |
| 46 | unsigned StartOffset = llvm::cantFail(positionToOffset(Input, T.R.start)); |
| 47 | unsigned EndOffset = llvm::cantFail(positionToOffset(Input, T.R.end)); |
| 48 | assert(StartOffset <= EndOffset); |
| 49 | assert(NextChar <= StartOffset); |
| 50 | |
| 51 | bool hasDef = |
| 52 | T.Modifiers & (1 << uint32_t(HighlightingModifier::Definition)); |
| 53 | bool hasDecl = |
| 54 | T.Modifiers & (1 << uint32_t(HighlightingModifier::Declaration)); |
| 55 | EXPECT_TRUE(!hasDef || hasDecl); |
| 56 | |
| 57 | OS << Input.substr(NextChar, StartOffset - NextChar); |
| 58 | OS << '$' << T.Kind; |
| 59 | for (unsigned I = 0; |
| 60 | I <= static_cast<uint32_t>(HighlightingModifier::LastModifier); ++I) { |
| 61 | if (T.Modifiers & (1 << I)) { |
| 62 | // _decl_def is common and redundant, just print _def instead. |
| 63 | if (I != uint32_t(HighlightingModifier::Declaration) || !hasDef) |
| 64 | OS << '_' << static_cast<HighlightingModifier>(I); |
| 65 | } |
| 66 | } |
| 67 | OS << "[[" << Input.substr(StartOffset, EndOffset - StartOffset) << "]]"; |
| 68 | NextChar = EndOffset; |
| 69 | } |
| 70 | OS << Input.substr(NextChar); |
| 71 | return std::move(OS.str()); |
| 72 | } |
| 73 | |
| 74 | void checkHighlightings(llvm::StringRef Code, |
| 75 | std::vector<std::pair</*FileName*/ llvm::StringRef, |
| 76 | /*FileContent*/ llvm::StringRef>> |
| 77 | AdditionalFiles = {}, |
| 78 | uint32_t ModifierMask = -1, |
| 79 | std::vector<std::string> AdditionalArgs = {}) { |
| 80 | Annotations Test(Code); |
| 81 | TestTU TU; |
| 82 | TU.Code = std::string(Test.code()); |
| 83 | |
| 84 | TU.ExtraArgs.push_back("-std=c++20"); |
| 85 | TU.ExtraArgs.push_back("-xobjective-c++"); |
| 86 | TU.ExtraArgs.insert(std::end(TU.ExtraArgs), std::begin(AdditionalArgs), |
| 87 | std::end(AdditionalArgs)); |
| 88 | |
| 89 | for (auto File : AdditionalFiles) |
| 90 | TU.AdditionalFiles.insert({File.first, std::string(File.second)}); |
| 91 | auto AST = TU.build(); |
| 92 | auto Actual = getSemanticHighlightings(AST); |
| 93 | for (auto &Token : Actual) |
| 94 | Token.Modifiers &= ModifierMask; |
| 95 | |
| 96 | EXPECT_EQ(Code, annotate(Test.code(), Actual)); |
| 97 | } |
| 98 | |
| 99 | constexpr static uint32_t ScopeModifierMask = |
| 100 | 1 << unsigned(HighlightingModifier::FunctionScope) | |
| 101 | 1 << unsigned(HighlightingModifier::ClassScope) | |
| 102 | 1 << unsigned(HighlightingModifier::FileScope) | |
| 103 | 1 << unsigned(HighlightingModifier::GlobalScope); |
| 104 | |
| 105 | TEST(SemanticHighlighting, GetsCorrectTokens) { |
| 106 | const char *TestCases[] = { |
| 107 | R"cpp( |
| 108 | struct $Class_def[[AS]] { |
| 109 | double $Field_decl[[SomeMember]]; |
| 110 | }; |
| 111 | struct { |
| 112 | } $Variable_def[[S]]; |
| 113 | void $Function_def[[foo]](int $Parameter_def[[A]], $Class[[AS]] $Parameter_def[[As]]) { |
| 114 | $Primitive_deduced_defaultLibrary[[auto]] $LocalVariable_def[[VeryLongVariableName]] = 12312; |
| 115 | $Class[[AS]] $LocalVariable_def[[AA]]; |
| 116 | $Primitive_deduced_defaultLibrary[[auto]] $LocalVariable_def[[L]] = $LocalVariable[[AA]].$Field[[SomeMember]] $Operator[[+]] $Parameter[[A]]; |
| 117 | auto $LocalVariable_def[[FN]] = [ $LocalVariable[[AA]]](int $Parameter_def[[A]]) -> void {}; |
| 118 | $LocalVariable[[FN]]$Operator_userDefined[[(]]12312$Operator_userDefined[[)]]; |
| 119 | } |
| 120 | )cpp", |
| 121 | R"cpp( |
| 122 | void $Function_decl[[foo]](int); |
| 123 | void $Function_decl[[Gah]](); |
| 124 | void $Function_def[[foo]]() { |
| 125 | auto $LocalVariable_def[[Bou]] = $Function[[Gah]]; |
| 126 | } |
| 127 | struct $Class_def[[A]] { |
| 128 | void $Method_decl[[abc]](); |
| 129 | }; |
| 130 | )cpp", |
| 131 | R"cpp( |
| 132 | namespace $Namespace_decl[[abc]] { |
| 133 | template<typename $TemplateParameter_def[[T]]> |
| 134 | struct $Class_def[[A]] { |
| 135 | $TemplateParameter[[T]] $Field_decl[[t]]; |
| 136 | }; |
| 137 | } |
| 138 | template<typename $TemplateParameter_def[[T]]> |
| 139 | struct $Class_def[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> { |
| 140 | typename $TemplateParameter[[T]]::$Type_dependentName[[A]]* $Field_decl[[D]]; |
| 141 | }; |
| 142 | $Namespace[[abc]]::$Class[[A]]<int> $Variable_def[[AA]]; |
| 143 | typedef $Namespace[[abc]]::$Class[[A]]<int> $Class_decl[[AAA]]; |
| 144 | struct $Class_def[[B]] { |
| 145 | $Class_decl_constrDestr[[B]](); |
| 146 | ~$Class_decl_constrDestr[[B]](); |
| 147 | void operator$Operator_decl[[<<]]($Class[[B]]); |
| 148 | $Class[[AAA]] $Field_decl[[AA]]; |
| 149 | }; |
| 150 | $Class[[B]]::$Class_def_constrDestr[[B]]() {} |
| 151 | $Class[[B]]::~$Class_def_constrDestr[[B]]() {} |
| 152 | void $Function_def[[f]] () { |
| 153 | $Class[[B]] $LocalVariable_def[[BB]] = $Class[[B]](); |
| 154 | $LocalVariable[[BB]].~$Class_constrDestr[[B]](); |
| 155 | $Class[[B]](); |
| 156 | } |
| 157 | )cpp", |
| 158 | R"cpp( |
| 159 | enum class $Enum_decl[[E]] { |
| 160 | $EnumConstant_decl_readonly[[A]], |
| 161 | $EnumConstant_decl_readonly[[B]], |
| 162 | }; |
| 163 | enum $Enum_decl[[EE]] { |
| 164 | $EnumConstant_decl_readonly[[Hi]], |
| 165 | }; |
| 166 | struct $Class_def[[A]] { |
| 167 | $Enum[[E]] $Field_decl[[EEE]]; |
| 168 | $Enum[[EE]] $Field_decl[[EEEE]]; |
| 169 | }; |
| 170 | int $Variable_def[[I]] = $EnumConstant_readonly[[Hi]]; |
| 171 | $Enum[[E]] $Variable_def[[L]] = $Enum[[E]]::$EnumConstant_readonly[[B]]; |
| 172 | )cpp", |
| 173 | R"cpp( |
| 174 | namespace $Namespace_decl[[abc]] { |
| 175 | namespace {} |
| 176 | namespace $Namespace_decl[[bcd]] { |
| 177 | struct $Class_def[[A]] {}; |
| 178 | namespace $Namespace_decl[[cde]] { |
| 179 | struct $Class_def[[A]] { |
| 180 | enum class $Enum_decl[[B]] { |
| 181 | $EnumConstant_decl_readonly[[Hi]], |
| 182 | }; |
| 183 | }; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | using namespace $Namespace[[abc]]::$Namespace[[bcd]]; |
| 188 | namespace $Namespace_decl[[vwz]] = |
| 189 | $Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]]; |
| 190 | $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable_def[[AA]]; |
| 191 | $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable_def[[AAA]] = |
| 192 | $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant_readonly[[Hi]]; |
| 193 | ::$Namespace[[vwz]]::$Class[[A]] $Variable_def[[B]]; |
| 194 | ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable_def[[BB]]; |
| 195 | )cpp", |
| 196 | R"cpp( |
| 197 | struct $Class_def[[D]] { |
| 198 | double $Field_decl[[C]]; |
| 199 | }; |
| 200 | struct $Class_def[[A]] { |
| 201 | double $Field_decl[[B]]; |
| 202 | $Class[[D]] $Field_decl[[E]]; |
| 203 | static double $StaticField_decl_static[[S]]; |
| 204 | static void $StaticMethod_def_static[[bar]]() {} |
| 205 | void $Method_def[[foo]]() { |
| 206 | $Field[[B]] $Operator[[=]] 123; |
| 207 | this->$Field[[B]] $Operator[[=]] 156; |
| 208 | this->$Method[[foo]](); |
| 209 | $Method[[foo]](); |
| 210 | $StaticMethod_static[[bar]](); |
| 211 | $StaticField_static[[S]] $Operator[[=]] 90.1; |
| 212 | } |
| 213 | }; |
| 214 | void $Function_def[[foo]]() { |
| 215 | $Class[[A]] $LocalVariable_def[[AA]]; |
| 216 | $LocalVariable[[AA]].$Field[[B]] $Operator[[+=]] 2; |
| 217 | $LocalVariable[[AA]].$Method[[foo]](); |
| 218 | $LocalVariable[[AA]].$Field[[E]].$Field[[C]]; |
| 219 | $Class[[A]]::$StaticField_static[[S]] $Operator[[=]] 90; |
| 220 | } |
| 221 | )cpp", |
| 222 | R"cpp( |
| 223 | struct $Class_def[[AA]] { |
| 224 | int $Field_decl[[A]]; |
| 225 | }; |
| 226 | int $Variable_def[[B]]; |
| 227 | $Class[[AA]] $Variable_def[[A]]{$Variable[[B]]}; |
| 228 | )cpp", |
| 229 | R"cpp( |
| 230 | namespace $Namespace_decl[[a]] { |
| 231 | struct $Class_def[[A]] {}; |
| 232 | typedef char $Primitive_decl[[C]]; |
| 233 | } |
| 234 | typedef $Namespace[[a]]::$Class[[A]] $Class_decl[[B]]; |
| 235 | using $Class_decl[[BB]] = $Namespace[[a]]::$Class[[A]]; |
| 236 | enum class $Enum_decl[[E]] {}; |
| 237 | typedef $Enum[[E]] $Enum_decl[[C]]; |
| 238 | typedef $Enum[[C]] $Enum_decl[[CC]]; |
| 239 | using $Enum_decl[[CD]] = $Enum[[CC]]; |
| 240 | $Enum[[CC]] $Function_decl[[f]]($Class[[B]]); |
| 241 | $Enum[[CD]] $Function_decl[[f]]($Class[[BB]]); |
| 242 | typedef $Namespace[[a]]::$Primitive[[C]] $Primitive_decl[[PC]]; |
| 243 | typedef float $Primitive_decl[[F]]; |
| 244 | )cpp", |
| 245 | R"cpp( |
| 246 | template<typename $TemplateParameter_def[[T]], typename = void> |
| 247 | class $Class_def[[A]] { |
| 248 | $TemplateParameter[[T]] $Field_decl[[AA]]; |
| 249 | $TemplateParameter[[T]] $Method_decl[[foo]](); |
| 250 | }; |
| 251 | template<class $TemplateParameter_def[[TT]]> |
| 252 | class $Class_def[[B]] { |
| 253 | $Class[[A]]<$TemplateParameter[[TT]]> $Field_decl[[AA]]; |
| 254 | }; |
| 255 | template<class $TemplateParameter_def[[TT]], class $TemplateParameter_def[[GG]]> |
| 256 | class $Class_def[[BB]] {}; |
| 257 | template<class $TemplateParameter_def[[T]]> |
| 258 | class $Class_def[[BB]]<$TemplateParameter[[T]], int> {}; |
| 259 | template<class $TemplateParameter_def[[T]]> |
| 260 | class $Class_def[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {}; |
| 261 | |
| 262 | template<template<class> class $TemplateParameter_def[[T]], class $TemplateParameter_def[[C]]> |
| 263 | $TemplateParameter[[T]]<$TemplateParameter[[C]]> $Function_decl[[f]](); |
| 264 | |
| 265 | template<typename> |
| 266 | class $Class_def[[Foo]] {}; |
| 267 | |
| 268 | template<typename $TemplateParameter_def[[T]]> |
| 269 | void $Function_decl[[foo]]($TemplateParameter[[T]] ...); |
| 270 | )cpp", |
| 271 | R"cpp( |
| 272 | template <class $TemplateParameter_def[[T]]> |
| 273 | struct $Class_def[[Tmpl]] {$TemplateParameter[[T]] $Field_decl[[x]] = 0;}; |
| 274 | extern template struct $Class_def[[Tmpl]]<float>; |
| 275 | template struct $Class_def[[Tmpl]]<double>; |
| 276 | )cpp", |
| 277 | // This test is to guard against highlightings disappearing when using |
| 278 | // conversion operators as their behaviour in the clang AST differ from |
| 279 | // other CXXMethodDecls. |
| 280 | R"cpp( |
| 281 | class $Class_def[[Foo]] {}; |
| 282 | struct $Class_def[[Bar]] { |
| 283 | explicit operator $Class[[Foo]]*() const; |
| 284 | explicit operator int() const; |
| 285 | operator $Class[[Foo]](); |
| 286 | }; |
| 287 | void $Function_def[[f]]() { |
| 288 | $Class[[Bar]] $LocalVariable_def[[B]]; |
| 289 | $Class[[Foo]] $LocalVariable_def[[F]] = $LocalVariable[[B]]; |
| 290 | $Class[[Foo]] *$LocalVariable_def[[FP]] = ($Class[[Foo]]*)$LocalVariable[[B]]; |
| 291 | int $LocalVariable_def[[I]] = (int)$LocalVariable[[B]]; |
| 292 | } |
| 293 | )cpp", |
| 294 | R"cpp( |
| 295 | struct $Class_def[[B]] {}; |
| 296 | struct $Class_def[[A]] { |
| 297 | $Class[[B]] $Field_decl[[BB]]; |
| 298 | $Class[[A]] &operator$Operator_decl[[=]]($Class[[A]] &&$Parameter_def[[O]]); |
| 299 | }; |
| 300 | |
| 301 | $Class[[A]] &$Class[[A]]::operator$Operator_def[[=]]($Class[[A]] &&$Parameter_def[[O]]) = default; |
| 302 | )cpp", |
| 303 | R"cpp( |
| 304 | enum $Enum_decl[[En]] { |
| 305 | $EnumConstant_decl_readonly[[EC]], |
| 306 | }; |
| 307 | class $Class_def[[Foo]] {}; |
| 308 | class $Class_def[[Bar]] { |
| 309 | public: |
| 310 | $Class[[Foo]] $Field_decl[[Fo]]; |
| 311 | $Enum[[En]] $Field_decl[[E]]; |
| 312 | int $Field_decl[[I]]; |
| 313 | $Class_def_constrDestr[[Bar]] ($Class[[Foo]] $Parameter_def[[F]], |
| 314 | $Enum[[En]] $Parameter_def[[E]]) |
| 315 | : $Field[[Fo]] ($Parameter[[F]]), $Field[[E]] ($Parameter[[E]]), |
| 316 | $Field[[I]] (123) {} |
| 317 | }; |
| 318 | class $Class_def[[Bar2]] : public $Class[[Bar]] { |
| 319 | $Class_def_constrDestr[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant_readonly[[EC]]) {} |
| 320 | }; |
| 321 | )cpp", |
| 322 | R"cpp( |
| 323 | enum $Enum_decl[[E]] { |
| 324 | $EnumConstant_decl_readonly[[E]], |
| 325 | }; |
| 326 | class $Class_def[[Foo]] {}; |
| 327 | $Enum_deduced[[auto]] $Variable_def[[AE]] = $Enum[[E]]::$EnumConstant_readonly[[E]]; |
| 328 | $Class_deduced[[auto]] $Variable_def[[AF]] = $Class[[Foo]](); |
| 329 | $Class_deduced[[decltype]](auto) $Variable_def[[AF2]] = $Class[[Foo]](); |
| 330 | $Class_deduced[[auto]] *$Variable_def[[AFP]] = $Operator[[&]]$Variable[[AF]]; |
| 331 | $Enum_deduced[[auto]] &$Variable_def[[AER]] = $Variable[[AE]]; |
| 332 | $Primitive_deduced_defaultLibrary[[auto]] $Variable_def[[Form]] = 10.2 $Operator[[+]] 2 $Operator[[*]] 4; |
| 333 | $Primitive_deduced_defaultLibrary[[decltype]]($Variable[[Form]]) $Variable_def[[F]] = 10; |
| 334 | auto $Variable_def[[Fun]] = []()->void{}; |
| 335 | )cpp", |
| 336 | R"cpp( |
| 337 | class $Class_def[[G]] {}; |
| 338 | template<$Class[[G]] *$TemplateParameter_def_readonly[[U]]> |
| 339 | class $Class_def[[GP]] {}; |
| 340 | template<$Class[[G]] &$TemplateParameter_def_readonly[[U]]> |
| 341 | class $Class_def[[GR]] {}; |
| 342 | template<int *$TemplateParameter_def_readonly[[U]]> |
| 343 | class $Class_def[[IP]] { |
| 344 | void $Method_def[[f]]() { |
| 345 | $Operator[[*]]$TemplateParameter_readonly[[U]] $Operator[[+=]] 5; |
| 346 | } |
| 347 | }; |
| 348 | template<unsigned $TemplateParameter_def_readonly[[U]] = 2> |
| 349 | class $Class_def[[Foo]] { |
| 350 | void $Method_def[[f]]() { |
| 351 | for(int $LocalVariable_def[[I]] = 0; |
| 352 | $LocalVariable[[I]] $Operator[[<]] $TemplateParameter_readonly[[U]];) {} |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | $Class[[G]] $Variable_def[[L]]; |
| 357 | void $Function_def[[f]]() { |
| 358 | $Class[[Foo]]<123> $LocalVariable_def[[F]]; |
| 359 | $Class[[GP]]<$Operator[[&]]$Variable[[L]]> $LocalVariable_def[[LL]]; |
| 360 | $Class[[GR]]<$Variable[[L]]> $LocalVariable_def[[LLL]]; |
| 361 | } |
| 362 | )cpp", |
| 363 | R"cpp( |
| 364 | template<typename $TemplateParameter_def[[T]], |
| 365 | void ($TemplateParameter[[T]]::*$TemplateParameter_def_readonly[[method]])(int)> |
| 366 | struct $Class_def[[G]] { |
| 367 | void $Method_def[[foo]]( |
| 368 | $TemplateParameter[[T]] *$Parameter_def[[O]]) { |
| 369 | ($Parameter[[O]]$Operator_userDefined[[->*]]$TemplateParameter_readonly[[method]])(10); |
| 370 | |
| 371 | } |
| 372 | }; |
| 373 | struct $Class_def[[F]] { |
| 374 | void $Method_decl[[f]](int); |
| 375 | }; |
| 376 | template<void (*$TemplateParameter_def_readonly[[Func]])()> |
| 377 | struct $Class_def[[A]] { |
| 378 | void $Method_def[[f]]() { |
| 379 | ($Operator[[*]]$TemplateParameter_readonly[[Func]])(); |
| 380 | } |
| 381 | }; |
| 382 | |
| 383 | void $Function_def[[foo]]() { |
| 384 | $Class[[F]] $LocalVariable_def[[FF]]; |
| 385 | $Class[[G]]<$Class[[F]], $Operator[[&]]$Class[[F]]::$Method[[f]]> $LocalVariable_def[[GG]]; |
| 386 | $LocalVariable[[GG]].$Method[[foo]]($Operator[[&]]$LocalVariable_usedAsMutablePointer[[FF]]); |
| 387 | $Class[[A]]<$Function[[foo]]> $LocalVariable_def[[AA]]; |
| 388 | } |
| 389 | )cpp", |
| 390 | // Tokens that share a source range but have conflicting Kinds are not |
| 391 | // highlighted. |
| 392 | R"cpp( |
| 393 | #define $Macro_decl[[DEF_MULTIPLE]](X) namespace X { class X { int X; }; } |
| 394 | #define $Macro_decl[[DEF_CLASS]](T) class T {}; |
| 395 | // Preamble ends. |
| 396 | $Macro[[DEF_MULTIPLE]](XYZ); |
| 397 | $Macro[[DEF_MULTIPLE]](XYZW); |
| 398 | $Macro[[DEF_CLASS]]($Class_def[[A]]) |
| 399 | #define $Macro_decl[[MACRO_CONCAT]](X, V, T) T foo##X = V |
| 400 | #define $Macro_decl[[DEF_VAR]](X, V) int X = V |
| 401 | #define $Macro_decl[[DEF_VAR_T]](T, X, V) T X = V |
| 402 | #define $Macro_decl[[DEF_VAR_REV]](V, X) DEF_VAR(X, V) |
| 403 | #define $Macro_decl[[CPY]](X) X |
| 404 | #define $Macro_decl[[DEF_VAR_TYPE]](X, Y) X Y |
| 405 | #define $Macro_decl[[SOME_NAME]] variable |
| 406 | #define $Macro_decl[[SOME_NAME_SET]] variable2 = 123 |
| 407 | #define $Macro_decl[[INC_VAR]](X) X += 2 |
| 408 | void $Function_def[[foo]]() { |
| 409 | $Macro[[DEF_VAR]]($LocalVariable_def[[X]], 123); |
| 410 | $Macro[[DEF_VAR_REV]](908, $LocalVariable_def[[XY]]); |
| 411 | int $Macro[[CPY]]( $LocalVariable_def[[XX]] ); |
| 412 | $Macro[[DEF_VAR_TYPE]]($Class[[A]], $LocalVariable_def[[AA]]); |
| 413 | double $Macro[[SOME_NAME]]; |
| 414 | int $Macro[[SOME_NAME_SET]]; |
| 415 | $LocalVariable[[variable]] $Operator[[=]] 20.1; |
| 416 | $Macro[[MACRO_CONCAT]](var, 2, float); |
| 417 | $Macro[[DEF_VAR_T]]($Class[[A]], $Macro[[CPY]]( |
| 418 | $Macro[[CPY]]($LocalVariable_def[[Nested]])), |
| 419 | $Macro[[CPY]]($Class[[A]]())); |
| 420 | $Macro[[INC_VAR]]($LocalVariable[[variable]]); |
| 421 | } |
| 422 | void $Macro[[SOME_NAME]](); |
| 423 | $Macro[[DEF_VAR]]($Variable_def[[MMMMM]], 567); |
| 424 | $Macro[[DEF_VAR_REV]](756, $Variable_def[[AB]]); |
| 425 | |
| 426 | #define $Macro_decl[[CALL_FN]](F) F(); |
| 427 | #define $Macro_decl[[DEF_FN]](F) void F () |
| 428 | $Macro[[DEF_FN]]($Function_def[[g]]) { |
| 429 | $Macro[[CALL_FN]]($Function[[foo]]); |
| 430 | } |
| 431 | )cpp", |
| 432 | R"cpp( |
| 433 | #define $Macro_decl[[fail]](expr) expr |
| 434 | #define $Macro_decl[[assert]](COND) if (!(COND)) { fail("assertion failed" #COND); } |
| 435 | // Preamble ends. |
| 436 | int $Variable_def[[x]]; |
| 437 | int $Variable_def[[y]]; |
| 438 | int $Function_decl[[f]](); |
| 439 | void $Function_def[[foo]]() { |
| 440 | $Macro[[assert]]($Variable[[x]] $Operator[[!=]] $Variable[[y]]); |
| 441 | $Macro[[assert]]($Variable[[x]] $Operator[[!=]] $Function[[f]]()); |
| 442 | } |
| 443 | )cpp", |
| 444 | // highlighting all macro references |
| 445 | R"cpp( |
| 446 | #ifndef $Macro[[name]] |
| 447 | #define $Macro_decl[[name]] |
| 448 | #endif |
| 449 | |
| 450 | #define $Macro_decl[[test]] |
| 451 | #undef $Macro[[test]] |
| 452 | $InactiveCode[[#ifdef test]] |
| 453 | $InactiveCode[[#endif]] |
| 454 | |
| 455 | $InactiveCode[[#if defined(test)]] |
| 456 | $InactiveCode[[#endif]] |
| 457 | )cpp", |
| 458 | R"cpp( |
| 459 | struct $Class_def[[S]] { |
| 460 | float $Field_decl[[Value]]; |
| 461 | $Class[[S]] *$Field_decl[[Next]]; |
| 462 | }; |
| 463 | $Class[[S]] $Variable_def[[Global]][2] = {$Class[[S]](), $Class[[S]]()}; |
| 464 | auto [$Variable_decl[[G1]], $Variable_decl[[G2]]] = $Variable[[Global]]; |
| 465 | void $Function_def[[f]]($Class[[S]] $Parameter_def[[P]]) { |
| 466 | int $LocalVariable_def[[A]][2] = {1,2}; |
| 467 | auto [$LocalVariable_decl[[B1]], $LocalVariable_decl[[B2]]] = $LocalVariable[[A]]; |
| 468 | auto [$LocalVariable_decl[[G1]], $LocalVariable_decl[[G2]]] = $Variable[[Global]]; |
| 469 | $Class_deduced[[auto]] [$LocalVariable_decl[[P1]], $LocalVariable_decl[[P2]]] = $Parameter[[P]]; |
| 470 | // Highlights references to BindingDecls. |
| 471 | $LocalVariable[[B1]]$Operator[[++]]; |
| 472 | } |
| 473 | )cpp", |
| 474 | R"cpp( |
| 475 | template<class $TemplateParameter_def[[T]]> |
| 476 | class $Class_def[[A]] { |
| 477 | using $TemplateParameter_decl[[TemplateParam1]] = $TemplateParameter[[T]]; |
| 478 | typedef $TemplateParameter[[T]] $TemplateParameter_decl[[TemplateParam2]]; |
| 479 | using $Primitive_decl[[IntType]] = int; |
| 480 | |
| 481 | using $Typedef_decl[[Pointer]] = $TemplateParameter[[T]] *; |
| 482 | using $Typedef_decl[[LVReference]] = $TemplateParameter[[T]] &; |
| 483 | using $Typedef_decl[[RVReference]] = $TemplateParameter[[T]]&&; |
| 484 | using $Typedef_decl[[Array]] = $TemplateParameter[[T]]*[3]; |
| 485 | using $Typedef_decl[[MemberPointer]] = int ($Class[[A]]::*)(int); |
| 486 | |
| 487 | // Use various previously defined typedefs in a function type. |
| 488 | void $Method_decl[[func]]( |
| 489 | $Typedef[[Pointer]], $Typedef[[LVReference]], $Typedef[[RVReference]], |
| 490 | $Typedef[[Array]], $Typedef[[MemberPointer]]); |
| 491 | }; |
| 492 | )cpp", |
| 493 | R"cpp( |
| 494 | template <class $TemplateParameter_def[[T]]> |
| 495 | void $Function_decl[[phase1]]($TemplateParameter[[T]]); |
| 496 | template <class $TemplateParameter_def[[T]]> |
| 497 | void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) { |
| 498 | $Function[[phase1]]($Parameter[[P]]); |
| 499 | $Unknown_dependentName[[phase2]]($Parameter[[P]]); |
| 500 | } |
| 501 | )cpp", |
| 502 | R"cpp( |
| 503 | class $Class_def[[A]] { |
| 504 | template <class $TemplateParameter_def[[T]]> |
| 505 | void $Method_decl[[bar]]($TemplateParameter[[T]]); |
| 506 | }; |
| 507 | |
| 508 | template <class $TemplateParameter_def[[U]]> |
| 509 | void $Function_def[[foo]]($TemplateParameter[[U]] $Parameter_def[[P]]) { |
| 510 | $Class[[A]]().$Method[[bar]]($Parameter[[P]]); |
| 511 | } |
| 512 | )cpp", |
| 513 | R"cpp( |
| 514 | struct $Class_def[[A]] { |
| 515 | template <class $TemplateParameter_def[[T]]> |
| 516 | static void $StaticMethod_decl_static[[foo]]($TemplateParameter[[T]]); |
| 517 | }; |
| 518 | |
| 519 | template <class $TemplateParameter_def[[T]]> |
| 520 | struct $Class_def[[B]] { |
| 521 | void $Method_def[[bar]]() { |
| 522 | $Class[[A]]::$StaticMethod_static[[foo]]($TemplateParameter[[T]]()); |
| 523 | } |
| 524 | }; |
| 525 | )cpp", |
| 526 | R"cpp( |
| 527 | template <class $TemplateParameter_def[[T]]> |
| 528 | void $Function_decl[[foo]](typename $TemplateParameter[[T]]::$Type_dependentName[[Type]] |
| 529 | = $TemplateParameter[[T]]::$Unknown_dependentName[[val]]); |
| 530 | )cpp", |
| 531 | R"cpp( |
| 532 | template <class $TemplateParameter_def[[T]]> |
| 533 | void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) { |
| 534 | $Parameter[[P]].$Unknown_dependentName[[Field]]; |
| 535 | } |
| 536 | )cpp", |
| 537 | R"cpp( |
| 538 | template <class $TemplateParameter_def[[T]]> |
| 539 | class $Class_def[[A]] { |
| 540 | int $Method_def[[foo]]() { |
| 541 | return $TemplateParameter[[T]]::$Unknown_dependentName[[Field]]; |
| 542 | } |
| 543 | }; |
| 544 | )cpp", |
| 545 | // Highlighting the using decl as the underlying using shadow decl. |
| 546 | R"cpp( |
| 547 | void $Function_decl[[foo]](); |
| 548 | using ::$Function[[foo]]; |
| 549 | )cpp", |
| 550 | // Highlighting of template template arguments. |
| 551 | R"cpp( |
| 552 | template <template <class> class $TemplateParameter_def[[TT]], |
| 553 | template <class> class ...$TemplateParameter_def[[TTs]]> |
| 554 | struct $Class_def[[Foo]] { |
| 555 | $Class[[Foo]]<$TemplateParameter[[TT]], $TemplateParameter[[TTs]]...> |
| 556 | *$Field_decl[[t]]; |
| 557 | }; |
| 558 | )cpp", |
| 559 | // Inactive code highlighting |
| 560 | R"cpp( |
| 561 | // Code in the preamble. |
| 562 | // Inactive lines get an empty InactiveCode token at the beginning. |
| 563 | $InactiveCode[[#ifdef test]] |
| 564 | $InactiveCode[[#endif]] |
| 565 | |
| 566 | // A declaration to cause the preamble to end. |
| 567 | int $Variable_def[[EndPreamble]]; |
| 568 | |
| 569 | // Code after the preamble. |
| 570 | // Code inside inactive blocks does not get regular highlightings |
| 571 | // because it's not part of the AST. |
| 572 | #define $Macro_decl[[test2]] |
| 573 | $InactiveCode[[#if defined(test)]] |
| 574 | $InactiveCode[[int Inactive2;]] |
| 575 | $InactiveCode[[#elif defined(test2)]] |
| 576 | int $Variable_def[[Active1]]; |
| 577 | $InactiveCode[[#else]] |
| 578 | $InactiveCode[[int Inactive3;]] |
| 579 | $InactiveCode[[#endif]] |
| 580 | |
| 581 | #ifndef $Macro[[test]] |
| 582 | int $Variable_def[[Active2]]; |
| 583 | #endif |
| 584 | |
| 585 | $InactiveCode[[#ifdef test]] |
| 586 | $InactiveCode[[int Inactive4;]] |
| 587 | $InactiveCode[[#else]] |
| 588 | int $Variable_def[[Active3]]; |
| 589 | #endif |
| 590 | )cpp", |
| 591 | // Argument to 'sizeof...' |
| 592 | R"cpp( |
| 593 | template <typename... $TemplateParameter_def[[Elements]]> |
| 594 | struct $Class_def[[TupleSize]] { |
| 595 | static const int $StaticField_decl_readonly_static[[size]] = |
| 596 | sizeof...($TemplateParameter[[Elements]]); |
| 597 | }; |
| 598 | )cpp", |
| 599 | // More dependent types |
| 600 | R"cpp( |
| 601 | template <typename $TemplateParameter_def[[T]]> |
| 602 | struct $Class_def[[Waldo]] { |
| 603 | using $Typedef_decl[[Location1]] = typename $TemplateParameter[[T]] |
| 604 | ::$Type_dependentName[[Resolver]]::$Type_dependentName[[Location]]; |
| 605 | using $Typedef_decl[[Location2]] = typename $TemplateParameter[[T]] |
| 606 | ::template $Type_dependentName[[Resolver]]<$TemplateParameter[[T]]> |
| 607 | ::$Type_dependentName[[Location]]; |
| 608 | using $Typedef_decl[[Location3]] = typename $TemplateParameter[[T]] |
| 609 | ::$Type_dependentName[[Resolver]] |
| 610 | ::template $Type_dependentName[[Location]]<$TemplateParameter[[T]]>; |
| 611 | static const int $StaticField_decl_readonly_static[[Value]] = $TemplateParameter[[T]] |
| 612 | ::$Type_dependentName[[Resolver]]::$Unknown_dependentName[[Value]]; |
| 613 | }; |
| 614 | )cpp", |
| 615 | // Dependent name with heuristic target |
| 616 | R"cpp( |
| 617 | template <typename> |
| 618 | struct $Class_def[[Foo]] { |
| 619 | int $Field_decl[[Waldo]]; |
| 620 | void $Method_def[[bar]]() { |
| 621 | $Class[[Foo]]().$Field_dependentName[[Waldo]]; |
| 622 | } |
| 623 | template <typename $TemplateParameter_def[[U]]> |
| 624 | void $Method_def[[bar1]]() { |
| 625 | $Class[[Foo]]<$TemplateParameter[[U]]>().$Field_dependentName[[Waldo]]; |
| 626 | } |
| 627 | |
| 628 | void $Method_decl[[Overload]](); |
| 629 | void $Method_decl_readonly[[Overload]]() const; |
| 630 | }; |
| 631 | template <typename $TemplateParameter_def[[T]]> |
| 632 | void $Function_def[[baz]]($Class[[Foo]]<$TemplateParameter[[T]]> $Parameter_def[[o]]) { |
| 633 | $Parameter[[o]].$Method_readonly_dependentName[[Overload]](); |
| 634 | } |
| 635 | )cpp", |
| 636 | // Concepts |
| 637 | R"cpp( |
| 638 | template <typename $TemplateParameter_def[[T]]> |
| 639 | concept $Concept_decl[[Fooable]] = |
| 640 | requires($TemplateParameter[[T]] $Parameter_def[[F]]) { |
| 641 | $Parameter[[F]].$Unknown_dependentName[[foo]](); |
| 642 | }; |
| 643 | template <typename $TemplateParameter_def[[T]]> |
| 644 | requires $Concept[[Fooable]]<$TemplateParameter[[T]]> |
| 645 | void $Function_def[[bar]]($TemplateParameter[[T]] $Parameter_def[[F]]) { |
| 646 | $Parameter[[F]].$Unknown_dependentName[[foo]](); |
| 647 | } |
| 648 | )cpp", |
| 649 | // Dependent template name |
| 650 | R"cpp( |
| 651 | template <template <typename> class> struct $Class_def[[A]] {}; |
| 652 | template <typename $TemplateParameter_def[[T]]> |
| 653 | using $Typedef_decl[[W]] = $Class[[A]]< |
| 654 | $TemplateParameter[[T]]::template $Class_dependentName[[Waldo]] |
| 655 | >; |
| 656 | )cpp", |
| 657 | R"cpp( |
| 658 | class $Class_def_abstract[[Abstract]] { |
| 659 | public: |
| 660 | virtual void $Method_decl_abstract_virtual[[pure]]() = 0; |
| 661 | virtual void $Method_decl_virtual[[impl]](); |
| 662 | }; |
| 663 | void $Function_def[[foo]]($Class_abstract[[Abstract]]* $Parameter_def[[A]]) { |
| 664 | $Parameter[[A]]->$Method_abstract_virtual[[pure]](); |
| 665 | $Parameter[[A]]->$Method_virtual[[impl]](); |
| 666 | } |
| 667 | )cpp", |
| 668 | R"cpp( |
| 669 | <:[deprecated]:> int $Variable_def_deprecated[[x]]; |
| 670 | )cpp", |
| 671 | R"cpp( |
| 672 | // ObjC: Classes and methods |
| 673 | @class $Class_decl[[Forward]]; |
| 674 | |
| 675 | @interface $Class_def[[Foo]] |
| 676 | @end |
| 677 | @interface $Class_def[[Bar]] : $Class[[Foo]] |
| 678 | -(id) $Method_decl[[x]]:(int)$Parameter_def[[a]] $Method_decl[[y]]:(int)$Parameter_def[[b]]; |
| 679 | +(instancetype)$StaticMethod_decl_static[[sharedInstance]]; |
| 680 | +(void) $StaticMethod_decl_static[[explode]]; |
| 681 | @end |
| 682 | @implementation $Class_def[[Bar]] |
| 683 | -(id) $Method_def[[x]]:(int)$Parameter_def[[a]] $Method_def[[y]]:(int)$Parameter_def[[b]] { |
| 684 | return self; |
| 685 | } |
| 686 | +(instancetype)$StaticMethod_def_static[[sharedInstance]] { return 0; } |
| 687 | +(void) $StaticMethod_def_static[[explode]] {} |
| 688 | @end |
| 689 | |
| 690 | void $Function_def[[m]]($Class[[Bar]] *$Parameter_def[[b]]) { |
| 691 | [$Parameter[[b]] $Method[[x]]:1 $Method[[y]]:2]; |
| 692 | [$Class[[Bar]] $StaticMethod_static[[explode]]]; |
| 693 | } |
| 694 | )cpp", |
| 695 | R"cpp( |
| 696 | // ObjC: Protocols |
| 697 | @protocol $Interface_def[[Protocol]] |
| 698 | @end |
| 699 | @protocol $Interface_def[[Protocol2]] <$Interface[[Protocol]]> |
| 700 | @end |
| 701 | @interface $Class_def[[Klass]] <$Interface[[Protocol]]> |
| 702 | @end |
| 703 | id<$Interface[[Protocol]]> $Variable_def[[x]]; |
| 704 | )cpp", |
| 705 | R"cpp( |
| 706 | // ObjC: Categories |
| 707 | @interface $Class_def[[Foo]] |
| 708 | @end |
| 709 | @interface $Class[[Foo]]($Namespace_def[[Bar]]) |
| 710 | @end |
| 711 | @implementation $Class[[Foo]]($Namespace_def[[Bar]]) |
| 712 | @end |
| 713 | )cpp", |
| 714 | R"cpp( |
| 715 | // ObjC: Properties and Ivars. |
| 716 | @interface $Class_def[[Foo]] { |
| 717 | int $Field_decl[[_someProperty]]; |
| 718 | } |
| 719 | @property(nonatomic, assign) int $Field_decl[[someProperty]]; |
| 720 | @property(readonly, class) $Class[[Foo]] *$Field_decl_readonly_static[[sharedInstance]]; |
| 721 | @end |
| 722 | @implementation $Class_def[[Foo]] |
| 723 | @synthesize someProperty = _someProperty; |
| 724 | - (int)$Method_def[[otherMethod]] { |
| 725 | return 0; |
| 726 | } |
| 727 | - (int)$Method_def[[doSomething]] { |
| 728 | $Class[[Foo]].$Field_static[[sharedInstance]].$Field[[someProperty]] $Operator[[=]] 1; |
| 729 | self.$Field[[someProperty]] $Operator[[=]] self.$Field[[someProperty]] $Operator[[+]] self.$Field[[otherMethod]] $Operator[[+]] 1; |
| 730 | self->$Field[[_someProperty]] $Operator[[=]] $Field[[_someProperty]] $Operator[[+]] 1; |
| 731 | } |
| 732 | @end |
| 733 | )cpp", |
| 734 | // Member imported from dependent base |
| 735 | R"cpp( |
| 736 | template <typename> struct $Class_def[[Base]] { |
| 737 | int $Field_decl[[member]]; |
| 738 | }; |
| 739 | template <typename $TemplateParameter_def[[T]]> |
| 740 | struct $Class_def[[Derived]] : $Class[[Base]]<$TemplateParameter[[T]]> { |
| 741 | using $Class[[Base]]<$TemplateParameter[[T]]>::$Field_dependentName[[member]]; |
| 742 | |
| 743 | void $Method_def[[method]]() { |
| 744 | (void)$Field_dependentName[[member]]; |
| 745 | } |
| 746 | }; |
| 747 | )cpp", |
| 748 | // Modifier for variables passed as non-const references |
| 749 | R"cpp( |
| 750 | struct $Class_def[[ClassWithOp]] { |
| 751 | void operator$Operator_decl[[(]]$Operator_decl[[)]](int); |
| 752 | void operator$Operator_decl[[(]]$Operator_decl[[)]](int, int &); |
| 753 | void operator$Operator_decl[[(]]$Operator_decl[[)]](int, int, const int &); |
| 754 | int &operator$Operator_decl[[[]]$Operator_decl[[]]](int &); |
| 755 | int operator$Operator_decl[[[]]$Operator_decl[[]]](int) const; |
| 756 | }; |
| 757 | struct $Class_def[[ClassWithStaticMember]] { |
| 758 | static inline int $StaticField_def_static[[j]] = 0; |
| 759 | }; |
| 760 | struct $Class_def[[ClassWithRefMembers]] { |
| 761 | $Class_def_constrDestr[[ClassWithRefMembers]](int $Parameter_def[[i]]) |
| 762 | : $Field[[i1]]($Parameter[[i]]), |
| 763 | $Field_readonly[[i2]]($Parameter[[i]]), |
| 764 | $Field[[i3]]($Parameter_usedAsMutableReference[[i]]), |
| 765 | $Field_readonly[[i4]]($Class[[ClassWithStaticMember]]::$StaticField_static[[j]]), |
| 766 | $Field[[i5]]($Class[[ClassWithStaticMember]]::$StaticField_static_usedAsMutableReference[[j]]) |
| 767 | {} |
| 768 | int $Field_decl[[i1]]; |
| 769 | const int &$Field_decl_readonly[[i2]]; |
| 770 | int &$Field_decl[[i3]]; |
| 771 | const int &$Field_decl_readonly[[i4]]; |
| 772 | int &$Field_decl[[i5]]; |
| 773 | }; |
| 774 | void $Function_def[[fun]](int, const int, |
| 775 | int*, const int*, |
| 776 | int&, const int&, |
| 777 | int*&, const int*&, const int* const &, |
| 778 | int**, int**&, int** const &, |
| 779 | int = 123) { |
| 780 | int $LocalVariable_def[[val]]; |
| 781 | int* $LocalVariable_def[[ptr]]; |
| 782 | const int* $LocalVariable_def_readonly[[constPtr]]; |
| 783 | int** $LocalVariable_def[[array]]; |
| 784 | $Function[[fun]]($LocalVariable[[val]], $LocalVariable[[val]], |
| 785 | $LocalVariable_usedAsMutablePointer[[ptr]], $LocalVariable_readonly[[constPtr]], |
| 786 | $LocalVariable_usedAsMutableReference[[val]], $LocalVariable[[val]], |
| 787 | |
| 788 | $LocalVariable_usedAsMutableReference[[ptr]], |
| 789 | $LocalVariable_readonly_usedAsMutableReference[[constPtr]], |
| 790 | $LocalVariable_readonly[[constPtr]], |
| 791 | |
| 792 | $LocalVariable_usedAsMutablePointer[[array]], $LocalVariable_usedAsMutableReference[[array]], |
| 793 | $LocalVariable[[array]] |
| 794 | ); |
| 795 | [](int){}$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]]; |
| 796 | [](int&){}$Operator_userDefined[[(]]$LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[)]]; |
| 797 | [](const int&){}$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]]; |
| 798 | $Class[[ClassWithOp]] $LocalVariable_def[[c]]; |
| 799 | const $Class[[ClassWithOp]] $LocalVariable_def_readonly[[c2]]; |
| 800 | $LocalVariable[[c]]$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]]; |
| 801 | $LocalVariable[[c]]$Operator_userDefined[[(]]0, $LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[)]]; |
| 802 | $LocalVariable[[c]]$Operator_userDefined[[(]]0, 0, $LocalVariable[[val]]$Operator_userDefined[[)]]; |
| 803 | $LocalVariable[[c]]$Operator_userDefined[[[]]$LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[]]]; |
| 804 | $LocalVariable_readonly[[c2]]$Operator_userDefined[[[]]$LocalVariable[[val]]$Operator_userDefined[[]]]; |
| 805 | } |
| 806 | struct $Class_def[[S]] { |
| 807 | $Class_def_constrDestr[[S]](int&) { |
| 808 | $Class[[S]] $LocalVariable_def[[s1]]($Field_usedAsMutableReference[[field]]); |
| 809 | $Class[[S]] $LocalVariable_def[[s2]]($LocalVariable[[s1]].$Field_usedAsMutableReference[[field]]); |
| 810 | |
| 811 | $Class[[S]] $LocalVariable_def[[s3]]($StaticField_static_usedAsMutableReference[[staticField]]); |
| 812 | $Class[[S]] $LocalVariable_def[[s4]]($Class[[S]]::$StaticField_static_usedAsMutableReference[[staticField]]); |
| 813 | } |
| 814 | int $Field_decl[[field]]; |
| 815 | static int $StaticField_decl_static[[staticField]]; |
| 816 | }; |
| 817 | template <typename $TemplateParameter_def[[X]]> |
| 818 | void $Function_def[[foo]]($TemplateParameter[[X]]& $Parameter_def[[x]]) { |
| 819 | // We do not support dependent types, so this one should *not* get the modifier. |
| 820 | $Function[[foo]]($Parameter[[x]]); |
| 821 | } |
| 822 | )cpp", |
| 823 | // init-captures |
| 824 | R"cpp( |
| 825 | void $Function_def[[foo]]() { |
| 826 | int $LocalVariable_def[[a]], $LocalVariable_def[[b]]; |
| 827 | [ $LocalVariable_def[[c]] = $LocalVariable[[a]], |
| 828 | $LocalVariable_def[[d]]($LocalVariable[[b]]) ]() {}$Operator_userDefined[[(]]$Operator_userDefined[[)]]; |
| 829 | } |
| 830 | )cpp", |
| 831 | // Enum base specifier |
| 832 | R"cpp( |
| 833 | using $Primitive_decl[[MyTypedef]] = int; |
| 834 | enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {}; |
| 835 | )cpp", |
| 836 | // Enum base specifier |
| 837 | R"cpp( |
| 838 | typedef int $Primitive_decl[[MyTypedef]]; |
| 839 | enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {}; |
| 840 | )cpp", |
| 841 | // Using enum |
| 842 | R"cpp( |
| 843 | enum class $Enum_decl[[Color]] { $EnumConstant_decl_readonly[[Black]] }; |
| 844 | namespace $Namespace_decl[[ns]] { |
| 845 | using enum $Enum[[Color]]; |
| 846 | $Enum[[Color]] $Variable_def[[ModelT]] = $EnumConstant[[Black]]; |
| 847 | } |
| 848 | )cpp", |
| 849 | // Issue 1096 |
| 850 | R"cpp( |
| 851 | void $Function_decl[[Foo]](); |
| 852 | // Use <: :> digraphs for deprecated attribute to avoid conflict with annotation syntax |
| 853 | <:<:deprecated:>:> void $Function_decl_deprecated[[Foo]](int* $Parameter_def[[x]]); |
| 854 | void $Function_decl[[Foo]](int $Parameter_def[[x]]); |
| 855 | template <typename $TemplateParameter_def[[T]]> |
| 856 | void $Function_def[[Bar]]($TemplateParameter[[T]] $Parameter_def[[x]]) { |
| 857 | $Function_deprecated[[Foo]]($Parameter[[x]]); |
| 858 | $Function_deprecated[[Foo]]($Parameter[[x]]); |
| 859 | $Function_deprecated[[Foo]]($Parameter[[x]]); |
| 860 | } |
| 861 | )cpp", |
| 862 | // Predefined identifiers |
| 863 | R"cpp( |
| 864 | void $Function_def[[Foo]]() { |
| 865 | const char *$LocalVariable_def_readonly[[s]] = $LocalVariable_readonly_static[[__func__]]; |
| 866 | } |
| 867 | )cpp", |
| 868 | // override and final |
| 869 | R"cpp( |
| 870 | class $Class_def_abstract[[Base]] { virtual void $Method_decl_abstract_virtual[[m]]() = 0; }; |
| 871 | class $Class_def[[override]] : public $Class_abstract[[Base]] { void $Method_decl_virtual[[m]]() $Modifier[[override]]; }; |
| 872 | class $Class_def[[final]] : public $Class[[override]] { void $Method_decl_virtual[[m]]() $Modifier[[override]] $Modifier[[final]]; }; |
| 873 | )cpp", |
| 874 | // Issue 1222: readonly modifier for generic parameter |
| 875 | R"cpp( |
| 876 | template <typename $TemplateParameter_def[[T]]> |
| 877 | auto $Function_def[[foo]](const $TemplateParameter[[T]] $Parameter_def_readonly[[template_type]], |
| 878 | const $TemplateParameter[[auto]] $Parameter_def_readonly[[auto_type]], |
| 879 | const int $Parameter_def_readonly[[explicit_type]]) { |
| 880 | return $Parameter_readonly[[template_type]] |
| 881 | $Operator_userDefined[[+]] $Parameter_readonly[[auto_type]] |
| 882 | $Operator_userDefined[[+]] $Parameter_readonly[[explicit_type]]; |
| 883 | } |
| 884 | )cpp", |
| 885 | // Explicit template specialization |
| 886 | R"cpp( |
| 887 | struct $Class_def[[Base]]{}; |
| 888 | template <typename $TemplateParameter_def[[T]]> |
| 889 | struct $Class_def[[S]] : public $Class[[Base]] {}; |
| 890 | template <> |
| 891 | struct $Class_def[[S]]<void> : public $Class[[Base]] {}; |
| 892 | |
| 893 | template <typename $TemplateParameter_def[[T]]> |
| 894 | $TemplateParameter[[T]] $Variable_def[[x]] = {}; |
| 895 | template <> |
| 896 | int $Variable_def[[x]]<int> = (int)sizeof($Class[[Base]]); |
| 897 | )cpp", |
| 898 | // operator calls in template |
| 899 | R"cpp( |
| 900 | template<typename $TemplateParameter_def[[T]]> class $Class_def[[C]] { |
| 901 | bool $Method_def[[compare]]($TemplateParameter[[T]] $Parameter_def[[t1]], $TemplateParameter[[T]] $Parameter_def[[t2]]) { return $Parameter[[t1]] $Operator_userDefined[[==]] $Parameter[[t2]]; } |
| 902 | $TemplateParameter[[T]] $Method_def[[deref]]($TemplateParameter[[T]] *$Parameter_def[[t]]) { return $Operator_userDefined[[*]]$Parameter[[t]]; } |
| 903 | }; |
| 904 | )cpp", |
| 905 | // new and delete |
| 906 | R"cpp( |
| 907 | struct $Class_def[[S]] { int *$Field_decl[[a]]; }; |
| 908 | void $Function_def[[f]]() { |
| 909 | $Class[[S]] *$LocalVariable_def[[s]] = $Operator[[new]] $Class[[S]]; |
| 910 | $LocalVariable[[s]]->$Field[[a]] $Operator[[=]] $Operator[[new]] int[10]; |
| 911 | $Operator[[delete]][] $LocalVariable[[s]]->$Field[[a]]; |
| 912 | $Operator[[delete]] $LocalVariable[[s]]; |
| 913 | } |
| 914 | )cpp", |
| 915 | // explicit operator invocation |
| 916 | R"cpp( |
| 917 | struct $Class_def[[S]] { |
| 918 | $Class[[S]] operator$Operator_decl[[+]](const $Class[[S]] &$Parameter_def_readonly[[other]]); |
| 919 | }; |
| 920 | void $Function_def[[f]]() { |
| 921 | $Class[[S]] $LocalVariable_def[[s]]; |
| 922 | $Class[[S]] $LocalVariable_def[[s2]] = $LocalVariable[[s]].operator$Operator_userDefined[[+]]($LocalVariable[[s]]); |
| 923 | } |
| 924 | )cpp", |
| 925 | // no crash |
| 926 | R"cpp( |
| 927 | struct $Class_def[[Foo]] { |
| 928 | void $Method_decl[[foo]](); |
| 929 | }; |
| 930 | |
| 931 | void $Function_def[[s]]($Class[[Foo]] $Parameter_def[[f]]) { |
| 932 | auto $LocalVariable_def[[k]] = $Operator[[&]]$Class[[Foo]]::$Method[[foo]]; |
| 933 | ($Parameter[[f]]$Operator[[.*]]$LocalVariable[[k]])(); // no crash on VisitCXXMemberCallExpr |
| 934 | } |
| 935 | )cpp", |
| 936 | R"cpp( |
| 937 | template<typename> |
| 938 | class $Class_def[[Foo]] {}; |
| 939 | |
| 940 | template<typename $TemplateParameter_def[[T]]> |
| 941 | void $Function_def[[k]]() { |
| 942 | auto $LocalVariable_def[[s]] = $Operator[[new]] $Class[[Foo]]<$TemplateParameter[[T]]>(); |
| 943 | $Operator[[delete]] $LocalVariable[[s]]; |
| 944 | } |
| 945 | )cpp"}; |
| 946 | for (const auto &TestCase : TestCases) |
| 947 | // Mask off scope modifiers to keep the tests manageable. |
| 948 | // They're tested separately. |
| 949 | checkHighlightings(TestCase, {}, ~ScopeModifierMask); |
| 950 | |
| 951 | checkHighlightings(R"cpp( |
| 952 | class $Class_def[[A]] { |
| 953 | #include "imp.h" |
| 954 | }; |
| 955 | )cpp", |
| 956 | {{"imp.h", R"cpp( |
| 957 | int someMethod(); |
| 958 | void otherMethod(); |
| 959 | )cpp"}}, |
| 960 | ~ScopeModifierMask); |
| 961 | |
| 962 | // A separate test for macros in headers. |
| 963 | checkHighlightings(R"cpp( |
| 964 | #include "imp.h" |
| 965 | $Macro[[DEFINE_Y]] |
| 966 | $Macro[[DXYZ_Y]](A); |
| 967 | )cpp", |
| 968 | {{"imp.h", R"cpp( |
| 969 | #define DXYZ(X) class X {}; |
| 970 | #define DXYZ_Y(Y) DXYZ(x##Y) |
| 971 | #define DEFINE(X) int X; |
| 972 | #define DEFINE_Y DEFINE(Y) |
| 973 | )cpp"}}, |
| 974 | ~ScopeModifierMask); |
| 975 | |
| 976 | checkHighlightings(R"cpp( |
| 977 | #include "SYSObject.h" |
| 978 | @interface $Class_defaultLibrary[[SYSObject]] ($Namespace_def[[UserCategory]]) |
| 979 | @property(nonatomic, readonly) int $Field_decl_readonly[[user_property]]; |
| 980 | @end |
| 981 | int $Function_def[[somethingUsingSystemSymbols]]() { |
| 982 | $Class_defaultLibrary[[SYSObject]] *$LocalVariable_def[[obj]] = [$Class_defaultLibrary[[SYSObject]] $StaticMethod_static_defaultLibrary[[new]]]; |
| 983 | return $LocalVariable[[obj]].$Field_defaultLibrary[[value]] $Operator[[+]] $LocalVariable[[obj]].$Field_readonly[[user_property]]; |
| 984 | } |
| 985 | )cpp", |
| 986 | {{"SystemSDK/SYSObject.h", R"cpp( |
| 987 | @interface SYSObject |
| 988 | @property(nonatomic, assign) int value; |
| 989 | + (instancetype)new; |
| 990 | @end |
| 991 | )cpp"}}, |
| 992 | ~ScopeModifierMask, {"-isystemSystemSDK/"}); |
| 993 | } |
| 994 | |
| 995 | TEST(SemanticHighlighting, ScopeModifiers) { |
| 996 | const char *TestCases[] = { |
| 997 | R"cpp( |
| 998 | static int $Variable_fileScope[[x]]; |
| 999 | namespace $Namespace_globalScope[[ns]] { |
| 1000 | class $Class_globalScope[[x]]; |
| 1001 | } |
| 1002 | namespace { |
| 1003 | void $Function_fileScope[[foo]](); |
| 1004 | } |
| 1005 | )cpp", |
| 1006 | R"cpp( |
| 1007 | void $Function_globalScope[[foo]](int $Parameter_functionScope[[y]]) { |
| 1008 | int $LocalVariable_functionScope[[z]]; |
| 1009 | } |
| 1010 | )cpp", |
| 1011 | R"cpp( |
| 1012 | // Lambdas are considered functions, not classes. |
| 1013 | auto $Variable_fileScope[[x]] = [$LocalVariable_functionScope[[m]](42)] { |
| 1014 | return $LocalVariable_functionScope[[m]]; |
| 1015 | }; |
| 1016 | )cpp", |
| 1017 | R"cpp( |
| 1018 | // Classes in functions are classes. |
| 1019 | void $Function_globalScope[[foo]]() { |
| 1020 | class $Class_functionScope[[X]] { |
| 1021 | int $Field_classScope[[x]]; |
| 1022 | }; |
| 1023 | }; |
| 1024 | )cpp", |
| 1025 | R"cpp( |
| 1026 | template <int $TemplateParameter_classScope[[T]]> |
| 1027 | class $Class_globalScope[[X]] { |
| 1028 | }; |
| 1029 | )cpp", |
| 1030 | R"cpp( |
| 1031 | // No useful scope for template parameters of variable templates. |
| 1032 | template <typename $TemplateParameter[[A]]> |
| 1033 | unsigned $Variable_globalScope[[X]] = |
| 1034 | $TemplateParameter[[A]]::$Unknown_classScope[[x]]; |
| 1035 | )cpp", |
| 1036 | R"cpp( |
| 1037 | #define $Macro_globalScope[[X]] 1 |
| 1038 | int $Variable_globalScope[[Y]] = $Macro_globalScope[[X]]; |
| 1039 | )cpp", |
| 1040 | }; |
| 1041 | |
| 1042 | for (const char *Test : TestCases) |
| 1043 | checkHighlightings(Test, {}, ScopeModifierMask); |
| 1044 | } |
| 1045 | |
| 1046 | // Ranges are highlighted as variables, unless highlighted as $Function etc. |
| 1047 | std::vector<HighlightingToken> tokens(llvm::StringRef MarkedText) { |
| 1048 | Annotations A(MarkedText); |
| 1049 | std::vector<HighlightingToken> Results; |
| 1050 | for (const Range& R : A.ranges()) |
| 1051 | Results.push_back({HighlightingKind::Variable, 0, R}); |
| 1052 | for (unsigned I = 0; I < static_cast<unsigned>(HighlightingKind::LastKind); ++I) { |
| 1053 | HighlightingKind Kind = static_cast<HighlightingKind>(I); |
| 1054 | for (const Range& R : A.ranges(llvm::to_string(Kind))) |
| 1055 | Results.push_back({Kind, 0, R}); |
| 1056 | } |
| 1057 | llvm::sort(Results); |
| 1058 | return Results; |
| 1059 | } |
| 1060 | |
| 1061 | TEST(SemanticHighlighting, toSemanticTokens) { |
| 1062 | auto Tokens = tokens(R"( |
| 1063 | [[blah]] |
| 1064 | |
| 1065 | $Function[[big]] [[bang]] |
| 1066 | )"); |
| 1067 | Tokens.front().Modifiers |= unsigned(HighlightingModifier::Declaration); |
| 1068 | Tokens.front().Modifiers |= unsigned(HighlightingModifier::Readonly); |
| 1069 | auto Results = toSemanticTokens(Tokens, /*Code=*/""); |
| 1070 | |
| 1071 | ASSERT_THAT(Results, SizeIs(3)); |
| 1072 | EXPECT_EQ(Results[0].tokenType, unsigned(HighlightingKind::Variable)); |
| 1073 | EXPECT_EQ(Results[0].tokenModifiers, |
| 1074 | unsigned(HighlightingModifier::Declaration) | |
| 1075 | unsigned(HighlightingModifier::Readonly)); |
| 1076 | EXPECT_EQ(Results[0].deltaLine, 1u); |
| 1077 | EXPECT_EQ(Results[0].deltaStart, 1u); |
| 1078 | EXPECT_EQ(Results[0].length, 4u); |
| 1079 | |
| 1080 | EXPECT_EQ(Results[1].tokenType, unsigned(HighlightingKind::Function)); |
| 1081 | EXPECT_EQ(Results[1].tokenModifiers, 0u); |
| 1082 | EXPECT_EQ(Results[1].deltaLine, 2u); |
| 1083 | EXPECT_EQ(Results[1].deltaStart, 4u); |
| 1084 | EXPECT_EQ(Results[1].length, 3u); |
| 1085 | |
| 1086 | EXPECT_EQ(Results[2].tokenType, unsigned(HighlightingKind::Variable)); |
| 1087 | EXPECT_EQ(Results[1].tokenModifiers, 0u); |
| 1088 | EXPECT_EQ(Results[2].deltaLine, 0u); |
| 1089 | EXPECT_EQ(Results[2].deltaStart, 4u); |
| 1090 | EXPECT_EQ(Results[2].length, 4u); |
| 1091 | } |
| 1092 | |
| 1093 | TEST(SemanticHighlighting, diffSemanticTokens) { |
| 1094 | auto Before = toSemanticTokens(tokens(R"( |
| 1095 | [[foo]] [[bar]] [[baz]] |
| 1096 | [[one]] [[two]] [[three]] |
| 1097 | )"), |
| 1098 | /*Code=*/""); |
| 1099 | EXPECT_THAT(diffTokens(Before, Before), IsEmpty()); |
| 1100 | |
| 1101 | auto After = toSemanticTokens(tokens(R"( |
| 1102 | [[foo]] [[hello]] [[world]] [[baz]] |
| 1103 | [[one]] [[two]] [[three]] |
| 1104 | )"), |
| 1105 | /*Code=*/""); |
| 1106 | |
| 1107 | // Replace [bar, baz] with [hello, world, baz] |
| 1108 | auto Diff = diffTokens(Before, After); |
| 1109 | ASSERT_THAT(Diff, SizeIs(1)); |
| 1110 | EXPECT_EQ(1u, Diff.front().startToken); |
| 1111 | EXPECT_EQ(2u, Diff.front().deleteTokens); |
| 1112 | ASSERT_THAT(Diff.front().tokens, SizeIs(3)); |
| 1113 | // hello |
| 1114 | EXPECT_EQ(0u, Diff.front().tokens[0].deltaLine); |
| 1115 | EXPECT_EQ(4u, Diff.front().tokens[0].deltaStart); |
| 1116 | EXPECT_EQ(5u, Diff.front().tokens[0].length); |
| 1117 | // world |
| 1118 | EXPECT_EQ(0u, Diff.front().tokens[1].deltaLine); |
| 1119 | EXPECT_EQ(6u, Diff.front().tokens[1].deltaStart); |
| 1120 | EXPECT_EQ(5u, Diff.front().tokens[1].length); |
| 1121 | // baz |
| 1122 | EXPECT_EQ(0u, Diff.front().tokens[2].deltaLine); |
| 1123 | EXPECT_EQ(6u, Diff.front().tokens[2].deltaStart); |
| 1124 | EXPECT_EQ(3u, Diff.front().tokens[2].length); |
| 1125 | } |
| 1126 | |
| 1127 | TEST(SemanticHighlighting, MultilineTokens) { |
| 1128 | llvm::StringRef AnnotatedCode = R"cpp( |
| 1129 | [[fo |
| 1130 | o |
| 1131 | o]] [[bar]])cpp"; |
| 1132 | auto Toks = toSemanticTokens(tokens(AnnotatedCode), |
| 1133 | Annotations(AnnotatedCode).code()); |
| 1134 | ASSERT_THAT(Toks, SizeIs(4)); |
| 1135 | // foo |
| 1136 | EXPECT_EQ(Toks[0].deltaLine, 1u); |
| 1137 | EXPECT_EQ(Toks[0].deltaStart, 2u); |
| 1138 | EXPECT_EQ(Toks[0].length, 2u); |
| 1139 | EXPECT_EQ(Toks[1].deltaLine, 1u); |
| 1140 | EXPECT_EQ(Toks[1].deltaStart, 0u); |
| 1141 | EXPECT_EQ(Toks[1].length, 1u); |
| 1142 | EXPECT_EQ(Toks[2].deltaLine, 1u); |
| 1143 | EXPECT_EQ(Toks[2].deltaStart, 0u); |
| 1144 | EXPECT_EQ(Toks[2].length, 1u); |
| 1145 | |
| 1146 | // bar |
| 1147 | EXPECT_EQ(Toks[3].deltaLine, 0u); |
| 1148 | EXPECT_EQ(Toks[3].deltaStart, 2u); |
| 1149 | EXPECT_EQ(Toks[3].length, 3u); |
| 1150 | } |
| 1151 | } // namespace |
| 1152 | } // namespace clangd |
| 1153 | } // namespace clang |