From: Daniel Dunbar Date: Sun, 14 Feb 2010 10:02:57 +0000 (+0000) Subject: CIndex: Switch CXSourceRange to proper half-open intervals. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d52864bd33c66aacc84133460d8c9c0dfcdd5c18;p=clang CIndex: Switch CXSourceRange to proper half-open intervals. - Doug, please review. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96162 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py index 58fd9e5384..8518765291 100644 --- a/bindings/python/tests/cindex/test_diagnostics.py +++ b/bindings/python/tests/cindex/test_diagnostics.py @@ -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 = ' diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index a42ec0669e..84ec4724e0 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -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. diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 5f8e25353d..4438622794 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -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, /// \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. diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 1619616bb7..543e444dbf 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -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; }