[clang] e159a3c - [Syntax] Remove a strict valid source location assertion for TypeLoc.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 11 00:58:46 PST 2021
Author: Haojian Wu
Date: 2021-02-11T09:53:52+01:00
New Revision: e159a3ced4c54b539c165b1cc26424fa0b34d53e
URL: https://github.com/llvm/llvm-project/commit/e159a3ced4c54b539c165b1cc26424fa0b34d53e
DIFF: https://github.com/llvm/llvm-project/commit/e159a3ced4c54b539c165b1cc26424fa0b34d53e.diff
LOG: [Syntax] Remove a strict valid source location assertion for TypeLoc.
The EndLoc of a type loc can be invalid for broken code.
Also extend the existing test to support error code with `error-ok`
annotation.
Differential Revision: https://reviews.llvm.org/D96261
Added:
Modified:
clang/lib/Tooling/Syntax/BuildTree.cpp
clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp
index 62573b6c95b2..a5cb293832d9 100644
--- a/clang/lib/Tooling/Syntax/BuildTree.cpp
+++ b/clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -294,11 +294,12 @@ static SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T,
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()) {
diff --git a/clang/unittests/Tooling/Syntax/BuildTreeTest.cpp b/clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
index a76557dcf803..299a2c320a4c 100644
--- a/clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
+++ b/clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
@@ -23,11 +23,9 @@ class BuildSyntaxTreeTest : public SyntaxTreeTest {
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
diff erent.
EXPECT_EQ(Tree.trim().str(), Actual);
@@ -45,11 +43,9 @@ class BuildSyntaxTreeTest : public SyntaxTreeTest {
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 @@ class BuildSyntaxTreeTest : public SyntaxTreeTest {
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 @@ TEST_P(BuildSyntaxTreeTest, ParametersAndQualifiers_InFreeFunctions_Named) {
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 @@ ParameterDeclarationList Parameters
`-DeclaratorList Declarators
`-SimpleDeclarator ListElement
`-'b'
+)txt",
+ R"txt(
+ParameterDeclarationList Parameters
+`-SimpleDeclaration ListElement
+ |-'undef'
+ `-DeclaratorList Declarators
+ `-SimpleDeclarator ListElement
+ `-'a'
)txt"}));
}
More information about the cfe-commits
mailing list