This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Do not end inactiveRegions range at position 0 of line
ClosedPublic

Authored by nridge on May 23 2023, 1:18 AM.

Details

Summary

This carries over the fix previously made for semantic highlighting
https://reviews.llvm.org/D92148, to the new inactiveRegions
protocol as well.

In addition, the directives at the beginning and end of an
inactive region are now excluded from the region.

Fixes https://github.com/clangd/clangd/issues/1631
Fixes https://github.com/clangd/clangd/issues/773

Diff Detail

Event Timeline

nridge created this revision.May 23 2023, 1:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 23 2023, 1:18 AM
nridge requested review of this revision.May 23 2023, 1:18 AM
hokein added inline comments.May 24 2023, 4:23 AM
clang-tools-extra/clangd/unittests/ClangdTests.cpp
1342–1343

While this patch is an improvement, I wonder we should move it further.

Has been thinking about it more, we seem to have some inconsistent behavior on highlighting the #if, #else, #endif lines:

  • in $inactive1 case, the #endif is marked as inactive (IMO, the highlighting in the editor is confusing, it looks like we're missing a match endif, thus I prefer to mark it as active to correspond to the active #if branch);
  • in $inactive3 case, the line #elif PREAMBLEMACRO > 0 is marked as inactive, this is inconsistent with $inactive1 case;

I think it would be nice to have a consistent model. One approach is to always consider #if, #elif, #endif, #endif lines active (in the implementation side, this would mean we always use the line range [skipp_range.start.line+1, skipp_range.end.line - 1]).

What do you think about this?

daiyousei-qz added inline comments.
clang-tools-extra/clangd/unittests/ClangdTests.cpp
1342–1343

+1. My perspective is that C++ source code is actually a meta-language (preprocessor) that describes generation of C++ language code. That is, #if, #else, #endif and .etc are always in a sense "executed" to generate the actual code. So they shouldn't be marked as inactive.

nridge planned changes to this revision.May 26 2023, 12:46 AM
nridge added inline comments.
clang-tools-extra/clangd/unittests/ClangdTests.cpp
1342–1343

I agree, what you describe is a nice additional improvement.

I believe it would also resolve the long-standing issue https://github.com/clangd/clangd/issues/773.

I will update the patch.

nridge updated this revision to Diff 526408.May 29 2023, 1:06 AM

Address review comments

nridge updated this revision to Diff 526409.May 29 2023, 1:09 AM

slight simplification

hokein added inline comments.May 29 2023, 10:24 PM
clang-tools-extra/clangd/ClangdServer.cpp
119–120

this part of code becomes non-trivial now, I suggest pulling out a function and moving it to SemanticHighlighting.cpp. The old inactive-as-comment implementation can share it as well.

153

nit: std::move(InactiveRegions).

clang-tools-extra/clangd/SemanticHighlighting.cpp
537–538

I think we should do the same thing for the old implementation as well (or just delete it at all), otherwise, we will have inconsistent behavior.

nridge updated this revision to Diff 528189.Jun 4 2023, 12:15 AM
nridge marked 3 inline comments as done.

Address review comments

nridge updated this revision to Diff 528190.Jun 4 2023, 12:17 AM

Add comment

nridge edited the summary of this revision. (Show Details)Jun 4 2023, 12:19 AM
hokein accepted this revision.Jun 5 2023, 12:14 AM

thanks, looks great.

clang-tools-extra/clangd/SourceCode.h
76 ↗(On Diff #528190)

nit: no need to expose this API, it is only used in SemanticHighlighting.cpp.

This revision is now accepted and ready to land.Jun 5 2023, 12:14 AM
nridge marked an inline comment as done.Jun 5 2023, 12:51 AM