[PATCH] D125863: [clangd] Dont mark terminating PP-directives as skipped

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 18 02:21:08 PDT 2022


kadircet created this revision.
kadircet added reviewers: sammccall, hokein.
Herald added subscribers: usaxena95, arphaman.
Herald added a project: All.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

PP-callback includes the terminating PP-directive
(else/elifdef/elifndef/endif) in the skipped source range. This results in
confusion as in theory that PP-directive is not skipped.
This patch changes the end location to be start of the line containing the
termination directive. That way clangd can keep highlightings for macro names in
the terminating directive as well.
This patch doesn't change the semantics of the PP-callback, as the range
possibly contains comments etc. trailing the termination directive. It's unclear
how useful that's for applications (only coverage mapping makes use of the full
range, rest always uses the endifloc for termination location), but it
definitely looks like a more intrusive change than just handling in clangd.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125863

Files:
  clang-tools-extra/clangd/CollectMacros.h
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ 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",
Index: clang-tools-extra/clangd/CollectMacros.h
===================================================================
--- clang-tools-extra/clangd/CollectMacros.h
+++ 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});
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125863.430294.patch
Type: text/x-patch
Size: 2059 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220518/003979d8/attachment.bin>


More information about the cfe-commits mailing list