From d391046930e437d4bb42cfa7c7d70f3302f93c7d Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Tue, 7 May 2013 04:29:22 +0000 Subject: [PATCH] Have SourceManager::getLocForEndOfFile() point at the "EOF" location of the FileID. This fixes a crash due to SourceManager::getLocForEndOfFile() returning an off-by-one location when the the FileID is for an empty file. rdar://13803893 llvm-svn: 181285 --- clang/include/clang/Basic/SourceManager.h | 2 +- clang/lib/Rewrite/Frontend/InclusionRewriter.cpp | 2 +- clang/test/Index/Inputs/empty.h | 0 clang/test/Index/file-includes.c | 3 +++ 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 clang/test/Index/Inputs/empty.h diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index f82b196929a3..eccbf1ede7f1 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1005,7 +1005,7 @@ public: return SourceLocation(); unsigned FileOffset = Entry.getOffset(); - return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1); + return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID)); } /// \brief Returns the include location if \p FID is a \#include'd file diff --git a/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp b/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp index 878be84224ae..9e4bb3c89bcf 100644 --- a/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp +++ b/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp @@ -464,7 +464,7 @@ bool InclusionRewriter::Process(FileID FileId, RawLex.LexFromRawLexer(RawToken); } OutputContentUpTo(FromFile, NextToWrite, - SM.getFileOffset(SM.getLocForEndOfFile(FileId)) + 1, EOL, Line, + SM.getFileOffset(SM.getLocForEndOfFile(FileId)), EOL, Line, /*EnsureNewline*/true); return true; } diff --git a/clang/test/Index/Inputs/empty.h b/clang/test/Index/Inputs/empty.h new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/clang/test/Index/file-includes.c b/clang/test/Index/file-includes.c index 2dfced0c0c4f..ac3d568dc53d 100644 --- a/clang/test/Index/file-includes.c +++ b/clang/test/Index/file-includes.c @@ -22,3 +22,6 @@ int LocalVar; // TOP: inclusion directive=targeted-nested1.h ({{.*[/\\]}}test{{[/\\]}}Index{{[/\\]}}targeted-nested1.h) =[5:1 - 5:2] // TOP: inclusion directive=targeted-fields.h ({{.*[/\\]}}test{{[/\\]}}Index{{[/\\]}}targeted-fields.h) =[16:1 - 16:2] + +// rdar://13803893 +// RUN: c-index-test -file-includes-in=%S/Inputs/empty.h %S/Inputs/empty.h