Commit 08fcb7f3 authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Filter out invalid diagnostic ranges

Apparently libclang might return invalid ranges.

Now we discard the invalid ranges. Since there is a diagnostic location
(in addition to ranges) the editor will still display an indication for
the user.

Task-number: QTCREATORBUG-15272
Change-Id: I351e136b9925a53fb2273a394e17873c5533798d
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
parent 8f2ad8d2
......@@ -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;
}
......
......@@ -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));
......
......@@ -44,6 +44,10 @@ class SourceRange
public:
SourceRange();
bool isNull() const;
bool isValid() const;
SourceLocation start() const;
SourceLocation end() const;
......
......@@ -7,3 +7,7 @@ int function(XXX i)
{
i + 20;
}
struct Foo {
someIdentifierLeadingToInvalidRange;
};
......@@ -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());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment