CIndex: Switch CXSourceRange to proper half-open intervals.

- Doug, please review.

llvm-svn: 96162
This commit is contained in:
Daniel Dunbar 2010-02-14 10:02:57 +00:00
parent edeb9b87e6
commit 02968e53cf
4 changed files with 20 additions and 26 deletions

View File

@ -44,5 +44,5 @@ def test_diagnostic_fixit():
assert tu.diagnostics[0].fixits[0].range.start.line == 1
assert tu.diagnostics[0].fixits[0].range.start.column == 26
assert tu.diagnostics[0].fixits[0].range.end.line == 1
assert tu.diagnostics[0].fixits[0].range.end.column == 29
assert tu.diagnostics[0].fixits[0].range.end.column == 30
assert tu.diagnostics[0].fixits[0].value == '.f0 = '

View File

@ -257,7 +257,7 @@ typedef struct {
} CXSourceLocation;
/**
* \brief Identifies a range of source locations in the source code.
* \brief Identifies a half-open character range in the source code.
*
* Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
* starting and end locations from a source range, respectively.

View File

@ -139,9 +139,11 @@ static RangeComparisonResult RangeCompare(SourceManager &SM,
SourceRange R2) {
assert(R1.isValid() && "First range is invalid?");
assert(R2.isValid() && "Second range is invalid?");
if (SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
if (R1.getEnd() == R2.getBegin() ||
SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
return RangeBefore;
if (SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
if (R2.getEnd() == R1.getBegin() ||
SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
return RangeAfter;
return RangeOverlap;
}
@ -180,9 +182,6 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
// Preprocessor::getLocForEndOfToken().
if (InstLoc.isValid()) {
unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, LangOpts);
// FIXME: Temporarily represent as closed range to preserve API
// compatibility.
if (Length) --Length;
EndLoc = EndLoc.getFileLocWithOffset(Length);
}
@ -235,7 +234,7 @@ class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
/// \brief Determine whether this particular source range comes before, comes
/// after, or overlaps the region of interest.
///
/// \param R a source range retrieved from the abstract syntax tree.
/// \param R a half-open source range retrieved from the abstract syntax tree.
RangeComparisonResult CompareRegionOfInterest(SourceRange R);
public:
@ -319,12 +318,6 @@ public:
} // end anonymous namespace
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
// Move the end of the input range to the end of the last token in that
// range.
SourceLocation NewEnd
= TU->getPreprocessor().getLocForEndOfToken(R.getEnd(), 1);
if (NewEnd.isValid())
R.setEnd(NewEnd);
return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
}
@ -444,12 +437,15 @@ bool CursorVisitor::VisitChildren(CXCursor Cursor) {
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
for (DeclContext::decl_iterator
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
CXCursor Cursor = MakeCXCursor(*I, TU);
if (RegionOfInterest.isValid()) {
SourceRange R = (*I)->getSourceRange();
if (R.isInvalid())
SourceRange Range =
cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
if (Range.isInvalid())
continue;
switch (CompareRegionOfInterest(R)) {
switch (CompareRegionOfInterest(Range)) {
case RangeBefore:
// This declaration comes before the region of interest; skip it.
continue;
@ -464,7 +460,7 @@ bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
}
}
if (Visit(MakeCXCursor(*I, TU), true))
if (Visit(Cursor, true))
return true;
}
@ -1194,7 +1190,7 @@ CXSourceRange clang_getNullRange() {
CXSourceRange Result = { { 0, 0 }, 0, 0 };
return Result;
}
CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
if (begin.ptr_data[0] != end.ptr_data[0] ||
begin.ptr_data[1] != end.ptr_data[1])
@ -1468,7 +1464,7 @@ CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
if (SLoc.isValid()) {
SourceRange RegionOfInterest(SLoc, SLoc);
SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
// FIXME: Would be great to have a "hint" cursor, then walk from that
// hint cursor upward until we find a cursor whose source range encloses
@ -2118,8 +2114,7 @@ void clang_annotateTokens(CXTranslationUnit TU,
SourceLocation End
= cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Tokens[NumTokens - 1]));
RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End,
1));
RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
// FIXME: Would be great to have a "hint" cursor, then walk from that
// hint cursor upward until we find a cursor whose source range encloses
// the region of interest, rather than starting from the translation unit.

View File

@ -34,9 +34,8 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
unsigned end_line, unsigned end_column) {
/* FIXME: Remove this + 1. */
fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
end_line, end_column + 1);
end_line, end_column);
}
static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
@ -616,7 +615,7 @@ static int perform_file_scan(const char *ast_file, const char *source_file,
if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
prevCursor.kind != CXCursor_InvalidFile) {
print_cursor_file_scan(prevCursor, start_line, start_col,
line, col - 1, prefix);
line, col, prefix);
start_line = line;
start_col = col;
}