diff --git a/clang-tools-extra/clangd/CollectMacros.h b/clang-tools-extra/clangd/CollectMacros.h --- a/clang-tools-extra/clangd/CollectMacros.h +++ b/clang-tools-extra/clangd/CollectMacros.h @@ -85,7 +85,9 @@ if (!InMainFile) return; Position Begin = sourceLocToPosition(SM, R.getBegin()); - Position End = sourceLocToPosition(SM, R.getEnd()); + Position End = sourceLocToPosition(SM, EndifLoc); + // Don't mark the terminating PP-directive as skipped. + End.character = 0; Out.SkippedRanges.push_back(Range{Begin, End}); } diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -441,10 +441,10 @@ #define $Macro_decl[[test]] #undef $Macro[[test]] $InactiveCode[[#ifdef test]] -$InactiveCode[[#endif]] +#endif $InactiveCode[[#if defined(test)]] -$InactiveCode[[#endif]] +#endif )cpp", R"cpp( struct $Class_decl[[S]] { @@ -551,8 +551,8 @@ R"cpp( // Code in the preamble. // Inactive lines get an empty InactiveCode token at the beginning. -$InactiveCode[[#ifdef test]] -$InactiveCode[[#endif]] +$InactiveCode[[#ifdef test // With comment]] +#endif // A declaration to cause the preamble to end. int $Variable_decl[[EndPreamble]]; @@ -563,11 +563,11 @@ #define $Macro_decl[[test2]] $InactiveCode[[#if defined(test)]] $InactiveCode[[int Inactive2;]] -$InactiveCode[[#elif defined(test2)]] +#elif defined($Macro[[test2]]) int $Variable_decl[[Active1]]; $InactiveCode[[#else]] $InactiveCode[[int Inactive3;]] -$InactiveCode[[#endif]] +#endif #ifndef $Macro[[test]] int $Variable_decl[[Active2]]; @@ -575,7 +575,7 @@ $InactiveCode[[#ifdef test]] $InactiveCode[[int Inactive4;]] -$InactiveCode[[#else]] +#else int $Variable_decl[[Active3]]; #endif )cpp",