[clang-tools-extra] r368287 - [clangd] Added an early return from VisitMemberExpr in SemanticHighlighting if underlying MemberDecl is a CXXConversionDecl.

Johan Vikstrom via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 8 05:43:55 PDT 2019


Author: jvikstrom
Date: Thu Aug  8 05:43:55 2019
New Revision: 368287

URL: http://llvm.org/viewvc/llvm-project?rev=368287&view=rev
Log:
[clangd] Added an early return from VisitMemberExpr in SemanticHighlighting if underlying MemberDecl is a CXXConversionDecl.

Summary:
Conversion operators contain invalid MemberLocs which caused SemanticHighlighting
to emit a lot of error logs in large files as they can occur fairly
often (for example converting StringRef to std string).
As the only thing happening was a lot of error logs being
emited there doesn't really seem to be any way to test this
(no erroneous tokens are added). But emiting as many logs as
were being emited is not wanted.

This also adds a test to guard against regressions for highlightings
disapearing from places where the conversion operators are used as their
behaviour differ from the other CXXMethodDecls.

Reviewers: hokein, ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D65928

Modified:
    clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
    clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368287&r1=368286&r2=368287&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Aug  8 05:43:55 2019
@@ -52,6 +52,10 @@ public:
       // When calling the destructor manually like: AAA::~A(); The ~ is a
       // MemberExpr. Other methods should still be highlighted though.
       return true;
+    if (isa<CXXConversionDecl>(MD))
+      // The MemberLoc is invalid for C++ conversion operators. We do not
+      // attempt to add tokens with invalid locations.
+      return true;
     addToken(ME->getMemberLoc(), MD);
     return true;
   }

Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368287&r1=368286&r2=368287&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Thu Aug  8 05:43:55 2019
@@ -255,6 +255,23 @@ TEST(SemanticHighlighting, GetsCorrectTo
       struct $Class[[Tmpl]] {$TemplateParameter[[T]] $Field[[x]] = 0;};
       extern template struct $Class[[Tmpl]]<float>;
       template struct $Class[[Tmpl]]<double>;
+    )cpp",
+    // This test is to guard against highlightings disappearing when using
+    // conversion operators as their behaviour in the clang AST differ from
+    // other CXXMethodDecls.
+    R"cpp(
+      class $Class[[Foo]] {};
+      struct $Class[[Bar]] {
+        explicit operator $Class[[Foo]]*() const;
+        explicit operator int() const;
+        operator $Class[[Foo]]();
+      };
+      void $Function[[f]]() {
+        $Class[[Bar]] $Variable[[B]];
+        $Class[[Foo]] $Variable[[F]] = $Variable[[B]];
+        $Class[[Foo]] *$Variable[[FP]] = ($Class[[Foo]]*)$Variable[[B]];
+        int $Variable[[I]] = (int)$Variable[[B]];
+      }
     )cpp"};
   for (const auto &TestCase : TestCases) {
     checkHighlightings(TestCase);




More information about the cfe-commits mailing list