diff --git a/src/tools/clangbackend/ipcsource/diagnostic.cpp b/src/tools/clangbackend/ipcsource/diagnostic.cpp index f0220e795819bc3795f4df787ee95fbe3f802788..e851a7c730e2a07019e83cfdaa8b3539c1125499 100644 --- a/src/tools/clangbackend/ipcsource/diagnostic.cpp +++ b/src/tools/clangbackend/ipcsource/diagnostic.cpp @@ -106,13 +106,15 @@ DiagnosticSeverity Diagnostic::severity() const std::vector<SourceRange> Diagnostic::ranges() const { std::vector<SourceRange> ranges; - const uint rangesCount = clang_getDiagnosticNumRanges(cxDiagnostic); - ranges.reserve(rangesCount); - for (uint index = 0; index < rangesCount; ++index) - ranges.push_back(SourceRange(clang_getDiagnosticRange(cxDiagnostic, index))); + for (uint index = 0; index < rangesCount; ++index) { + const SourceRange sourceRange(clang_getDiagnosticRange(cxDiagnostic, index)); + + if (sourceRange.isValid()) + ranges.push_back(SourceRange(clang_getDiagnosticRange(cxDiagnostic, index))); + } return ranges; } diff --git a/src/tools/clangbackend/ipcsource/sourcerange.cpp b/src/tools/clangbackend/ipcsource/sourcerange.cpp index 4ea26ba1fedc48ba4ed6010f37a95da651fd010c..7375988b2bd7237790ab6f3da927cb4d41540582 100644 --- a/src/tools/clangbackend/ipcsource/sourcerange.cpp +++ b/src/tools/clangbackend/ipcsource/sourcerange.cpp @@ -39,6 +39,16 @@ SourceRange::SourceRange() { } +bool SourceRange::isNull() const +{ + return clang_Range_isNull(cxSourceRange); +} + +bool SourceRange::isValid() const +{ + return !isNull() && start().offset() < end().offset(); +} + SourceLocation SourceRange::start() const { return SourceLocation(clang_getRangeStart(cxSourceRange)); diff --git a/src/tools/clangbackend/ipcsource/sourcerange.h b/src/tools/clangbackend/ipcsource/sourcerange.h index cec9f6fa658e91c657de1a37d8e3b5a69090b60b..3c80e4e040db381747a8b5b9c2b340426b89ee0f 100644 --- a/src/tools/clangbackend/ipcsource/sourcerange.h +++ b/src/tools/clangbackend/ipcsource/sourcerange.h @@ -44,6 +44,10 @@ class SourceRange public: SourceRange(); + + bool isNull() const; + bool isValid() const; + SourceLocation start() const; SourceLocation end() const; diff --git a/tests/unit/unittest/data/diagnostic_source_range.cpp b/tests/unit/unittest/data/diagnostic_source_range.cpp index ae0ee72f5937056dadbc9cf3eb0997b244c5b64d..121d402ea89c3289da6145b510e3d47ee31e0bb3 100644 --- a/tests/unit/unittest/data/diagnostic_source_range.cpp +++ b/tests/unit/unittest/data/diagnostic_source_range.cpp @@ -7,3 +7,7 @@ int function(XXX i) { i + 20; } + +struct Foo { + someIdentifierLeadingToInvalidRange; +}; diff --git a/tests/unit/unittest/sourcerangetest.cpp b/tests/unit/unittest/sourcerangetest.cpp index 00a57d5d247c7094dd8854ac32731caa336977fa..df9c4a37099afb5d310b167d71bbe1f86e799be9 100644 --- a/tests/unit/unittest/sourcerangetest.cpp +++ b/tests/unit/unittest/sourcerangetest.cpp @@ -85,9 +85,24 @@ protected: translationUnits}; DiagnosticSet diagnosticSet{translationUnit.diagnostics()}; Diagnostic diagnostic{diagnosticSet.front()}; + Diagnostic diagnosticWithFilteredOutInvalidRange{diagnosticSet.at(1)}; ::SourceRange sourceRange{diagnostic.ranges().front()}; }; +TEST_F(SourceRange, IsNull) +{ + ::SourceRange sourceRange; + + ASSERT_TRUE(sourceRange.isNull()); +} + +TEST_F(SourceRange, IsNotNull) +{ + ::SourceRange sourceRange = diagnostic.ranges()[0]; + + ASSERT_FALSE(sourceRange.isNull()); +} + TEST_F(SourceRange, Size) { ASSERT_THAT(diagnostic.ranges().size(), 2); @@ -108,4 +123,10 @@ TEST_F(SourceRange, End) 6u, 44u)); } + +TEST_F(SourceRange, InvalidRangeIsFilteredOut) +{ + ASSERT_TRUE(diagnosticWithFilteredOutInvalidRange.ranges().empty()); +} + }