[PATCH] D80508: [AST] Fix the source range for TagDecl if there is no matched } brace.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 25 01:34:13 PDT 2020


hokein created this revision.
hokein added reviewers: sammccall, akyrtzi.
Herald added subscribers: usaxena95, kadircet, arphaman, dexonsmith, jkorous, ilya-biryukov.
Herald added a project: clang.

The AST is preserved when the TagDecl misses a } brace, but the source
range seems incorrect (just covers the name, not the body, because
RBraceLoc is invalid and we fallback to NameLoc). This breaks the
SelectionTree in clangd -- clangd skips the TagDecl entirely.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80508

Files:
  clang-tools-extra/clangd/unittests/SelectionTests.cpp
  clang/lib/AST/Decl.cpp
  clang/test/AST/ast-dump-invalid-brace.cpp


Index: clang/test/AST/ast-dump-invalid-brace.cpp
===================================================================
--- /dev/null
+++ clang/test/AST/ast-dump-invalid-brace.cpp
@@ -0,0 +1,6 @@
+// RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump -ast-dump-filter Test %s | FileCheck -strict-whitespace %s
+
+// CHECK: CXXRecordDecl {{.*}}:4:1, <invalid sloc>
+class Test {
+  int abc;
+// }
Index: clang/lib/AST/Decl.cpp
===================================================================
--- clang/lib/AST/Decl.cpp
+++ clang/lib/AST/Decl.cpp
@@ -4126,8 +4126,8 @@
 }
 
 SourceRange TagDecl::getSourceRange() const {
-  SourceLocation RBraceLoc = BraceRange.getEnd();
-  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
+  SourceLocation E = BraceRange.getBegin().isValid() ?
+                     BraceRange.getEnd() : getLocation();
   return SourceRange(getOuterLocStart(), E);
 }
 
Index: clang-tools-extra/clangd/unittests/SelectionTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -388,7 +388,26 @@
         void test(S2 s2) {
           s2[[-^>]]f();
         }
-      )cpp", "DeclRefExpr"} // DeclRefExpr to the "operator->" method.
+      )cpp",
+       "DeclRefExpr"}, // DeclRefExpr to the "operator->" method.
+
+      // broken cases, missing } brace.
+      {
+          R"cpp(
+            // error-ok: AST is still valid on missing } brace.
+            class ABC {
+            [[int ^a]];
+            // }
+          )cpp",
+          "FieldDecl"},
+      {
+          R"cpp(
+            // error-ok
+            enum Color {
+            [[^Black]],
+            //}
+          )cpp",
+          "EnumConstantDecl"},
   };
   for (const Case &C : Cases) {
     trace::TestTracer Tracer;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80508.265974.patch
Type: text/x-patch
Size: 1886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200525/c0b96784/attachment-0001.bin>


More information about the cfe-commits mailing list