diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2008-01-19 00:30:35 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2008-01-19 00:30:35 +0000 |
commit | 452b899c9f5fbcb8ddd130375b7982bed6b4d93f (patch) | |
tree | e1c203ab2c36ee759da3ccb8c2624a085243922a /Driver/RewriteTest.cpp | |
parent | a15c15a1938d210cb053a893ca51d5e13cb83c02 (diff) |
Patch to replace #import with #include.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46183 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Driver/RewriteTest.cpp')
-rw-r--r-- | Driver/RewriteTest.cpp | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index fb52dfe29f..d9af1f9743 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -161,7 +161,7 @@ namespace { // Syntactic Rewriting. void RewritePrologue(SourceLocation Loc); - void RewriteInclude(SourceLocation Loc); + void RewriteInclude(); void RewriteTabs(); void RewriteForwardClassDecl(ObjCClassDecl *Dcl); void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); @@ -293,10 +293,6 @@ void RewriteTest::HandleTopLevelDecl(Decl *D) { // If we have a decl in the main file, see if we should rewrite it. if (SM->getDecomposedFileLoc(Loc).first == MainFileID) return HandleDeclInMainFile(D); - - // Otherwise, see if there is a #import in the main file that should be - // rewritten. - //RewriteInclude(Loc); } /// HandleDeclInMainFile - This is called for each top-level decl defined in the @@ -334,6 +330,8 @@ RewriteTest::~RewriteTest() { // Rewrite tabs if we care. //RewriteTabs(); + RewriteInclude(); + // Rewrite Objective-c meta data* std::string ResultStr; RewriteImplementations(ResultStr); @@ -356,28 +354,31 @@ RewriteTest::~RewriteTest() { // Syntactic (non-AST) Rewriting Code //===----------------------------------------------------------------------===// -void RewriteTest::RewriteInclude(SourceLocation Loc) { - // Rip up the #include stack to the main file. - SourceLocation IncLoc = Loc, NextLoc = Loc; - do { - IncLoc = Loc; - Loc = SM->getLogicalLoc(NextLoc); - NextLoc = SM->getIncludeLoc(Loc); - } while (!NextLoc.isInvalid()); - - // Loc is now the location of the #include filename "foo" or <foo/bar.h>. - // IncLoc indicates the header that was included if it is useful. - IncLoc = SM->getLogicalLoc(IncLoc); - if (SM->getDecomposedFileLoc(Loc).first != MainFileID || - Loc == LastIncLoc) - return; - LastIncLoc = Loc; - - unsigned IncCol = SM->getColumnNumber(Loc); - SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1); - - // Replace the #import with #include. - Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include ")); +void RewriteTest::RewriteInclude() { + SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0); + std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); + const char *MainBufStart = MainBuf.first; + const char *MainBufEnd = MainBuf.second; + size_t ImportLen = strlen("import"); + size_t IncludeLen = strlen("include"); + + // Loop over the whole file, looking for tabs. + for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { + if (*BufPtr == '#') { + if (++BufPtr == MainBufEnd) + return; + while (*BufPtr == ' ' || *BufPtr == '\t') + if (++BufPtr == MainBufEnd) + return; + if (!strncmp(BufPtr, "import", ImportLen)) { + // replace import with include + SourceLocation ImportLoc = + LocStart.getFileLocWithOffset(BufPtr-MainBufStart); + Rewrite.ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); + BufPtr += ImportLen; + } + } + } } void RewriteTest::RewriteTabs() { |