[PATCH] D96261: [Syntax] Remove a strict valid source location assertion for TypeLoc.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 11 00:58:54 PST 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe159a3ced4c5: [Syntax] Remove a strict valid source location assertion for TypeLoc. (authored by hokein).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96261/new/

https://reviews.llvm.org/D96261

Files:
  clang/lib/Tooling/Syntax/BuildTree.cpp
  clang/unittests/Tooling/Syntax/BuildTreeTest.cpp


Index: clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
+++ clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
@@ -23,11 +23,9 @@
     SCOPED_TRACE(llvm::join(GetParam().getCommandLineArgs(), " "));
 
     auto *Root = buildTree(Code, GetParam());
-    if (Diags->getClient()->getNumErrors() != 0) {
-      return ::testing::AssertionFailure()
-             << "Source file has syntax errors, they were printed to the test "
-                "log";
-    }
+    auto ErrorOK = errorOK(Code);
+    if (!ErrorOK)
+      return ErrorOK;
     auto Actual = StringRef(Root->dump(Arena->getSourceManager())).trim().str();
     // EXPECT_EQ shows the diff between the two strings if they are different.
     EXPECT_EQ(Tree.trim().str(), Actual);
@@ -45,11 +43,9 @@
     auto AnnotatedCode = llvm::Annotations(CodeWithAnnotations);
     auto *Root = buildTree(AnnotatedCode.code(), GetParam());
 
-    if (Diags->getClient()->getNumErrors() != 0) {
-      return ::testing::AssertionFailure()
-             << "Source file has syntax errors, they were printed to the test "
-                "log";
-    }
+    auto ErrorOK = errorOK(AnnotatedCode.code());
+    if (!ErrorOK)
+      return ErrorOK;
 
     auto AnnotatedRanges = AnnotatedCode.ranges();
     if (AnnotatedRanges.size() != TreeDumps.size()) {
@@ -77,6 +73,19 @@
     return Failed ? ::testing::AssertionFailure()
                   : ::testing::AssertionSuccess();
   }
+
+private:
+  ::testing::AssertionResult errorOK(StringRef RawCode) {
+    if (!RawCode.contains("error-ok")) {
+      if (Diags->getClient()->getNumErrors() != 0) {
+        return ::testing::AssertionFailure()
+               << "Source file has syntax errors (suppress with /*error-ok*/), "
+                  "they were printed to the "
+                  "test log";
+      }
+    }
+    return ::testing::AssertionSuccess();
+  }
 };
 
 INSTANTIATE_TEST_CASE_P(SyntaxTreeTests, BuildSyntaxTreeTest,
@@ -4773,6 +4782,7 @@
      int func1([[int a]]);
      int func2([[int *ap]]);
      int func3([[int a, float b]]);
+     int func4([[undef a]]); // error-ok: no crash on invalid type
    )cpp",
       {R"txt(
 ParameterDeclarationList Parameters
@@ -4804,6 +4814,14 @@
   `-DeclaratorList Declarators
     `-SimpleDeclarator ListElement
       `-'b'
+)txt",
+       R"txt(
+ParameterDeclarationList Parameters
+`-SimpleDeclaration ListElement
+  |-'undef'
+  `-DeclaratorList Declarators
+    `-SimpleDeclarator ListElement
+      `-'a'
 )txt"}));
 }
 
Index: clang/lib/Tooling/Syntax/BuildTree.cpp
===================================================================
--- clang/lib/Tooling/Syntax/BuildTree.cpp
+++ clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -294,11 +294,12 @@
                                       SourceRange Initializer) {
   SourceLocation Start = GetStartLoc().Visit(T);
   SourceLocation End = T.getEndLoc();
-  assert(End.isValid());
   if (Name.isValid()) {
     if (Start.isInvalid())
       Start = Name;
-    if (SM.isBeforeInTranslationUnit(End, Name))
+    // End of TypeLoc could be invalid if the type is invalid, fallback to the
+    // NameLoc.
+    if (End.isInvalid() || SM.isBeforeInTranslationUnit(End, Name))
       End = Name;
   }
   if (Initializer.isValid()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96261.322914.patch
Type: text/x-patch
Size: 3354 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210211/0f2b25d3/attachment.bin>


More information about the cfe-commits mailing list