From 7ee5d5f97b3fa709038ff7fd640dc775efaadc26 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 21 Jun 2009 05:06:04 +0000 Subject: [PATCH] move include searching logic from TGLexer to SourceMgr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73845 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/SourceMgr.h | 13 +++++++++++++ lib/Support/SourceMgr.cpp | 20 ++++++++++++++++++++ utils/TableGen/TGLexer.cpp | 17 ++++------------- utils/TableGen/TGLexer.h | 7 ------- utils/TableGen/TGParser.h | 2 -- utils/TableGen/TableGen.cpp | 6 +++--- 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/include/llvm/Support/SourceMgr.h b/include/llvm/Support/SourceMgr.h index 953cf897b99..53e7dd8dfbc 100644 --- a/include/llvm/Support/SourceMgr.h +++ b/include/llvm/Support/SourceMgr.h @@ -57,12 +57,20 @@ class SourceMgr { /// Buffers - This is all of the buffers that we are reading from. std::vector Buffers; + // IncludeDirectories - This is the list of directories we should search for + // include files in. + std::vector IncludeDirectories; + SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT void operator=(const SourceMgr&); // DO NOT IMPLEMENT public: SourceMgr() {} ~SourceMgr(); + void setIncludeDirs(const std::vector &Dirs) { + IncludeDirectories = Dirs; + } + const SrcBuffer &getBufferInfo(unsigned i) const { assert(i < Buffers.size() && "Invalid Buffer ID!"); return Buffers[i]; @@ -86,6 +94,11 @@ public: return Buffers.size()-1; } + /// AddIncludeFile - Search for a file with the specified name in the current + /// directory or in one of the IncludeDirs. If no file is found, this returns + /// ~0, otherwise it returns the buffer ID of the stacked file. + unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc); + /// FindBufferContainingLoc - Return the ID of the buffer containing the /// specified location, returning -1 if not found. int FindBufferContainingLoc(SMLoc Loc) const; diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index 40a6f43f5e0..5460cb3824d 100644 --- a/lib/Support/SourceMgr.cpp +++ b/lib/Support/SourceMgr.cpp @@ -25,6 +25,26 @@ SourceMgr::~SourceMgr() { } } +/// AddIncludeFile - Search for a file with the specified name in the current +/// directory or in one of the IncludeDirs. If no file is found, this returns +/// ~0, otherwise it returns the buffer ID of the stacked file. +unsigned SourceMgr::AddIncludeFile(const std::string &Filename, + SMLoc IncludeLoc) { + + MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str()); + + // If the file didn't exist directly, see if it's in an include path. + for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { + std::string IncFile = IncludeDirectories[i] + "/" + Filename; + NewBuf = MemoryBuffer::getFile(IncFile.c_str()); + } + + if (NewBuf == 0) return ~0U; + + return AddNewSourceBuffer(NewBuf, IncludeLoc); +} + + /// FindBufferContainingLoc - Return the ID of the buffer containing the /// specified location, returning -1 if not found. int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { diff --git a/utils/TableGen/TGLexer.cpp b/utils/TableGen/TGLexer.cpp index 578930c85f8..a1ccdbe68fc 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/utils/TableGen/TGLexer.cpp @@ -278,24 +278,15 @@ bool TGLexer::LexInclude() { // Get the string. std::string Filename = CurStrVal; - // Try to find the file. - MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str()); - - // If the file didn't exist directly, see if it's in an include path. - for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { - std::string IncFile = IncludeDirectories[i] + "/" + Filename; - NewBuf = MemoryBuffer::getFile(IncFile.c_str()); - } - - if (NewBuf == 0) { + + CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr)); + if (CurBuffer == ~0U) { PrintError(getLoc(), "Could not find include file '" + Filename + "'"); return true; } // Save the line number and lex buffer of the includer. - CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, SMLoc::getFromPointer(CurPtr)); - - CurBuf = NewBuf; + CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); CurPtr = CurBuf->getBufferStart(); return false; } diff --git a/utils/TableGen/TGLexer.h b/utils/TableGen/TGLexer.h index 38a1d2f2ee0..2d1958e8800 100644 --- a/utils/TableGen/TGLexer.h +++ b/utils/TableGen/TGLexer.h @@ -73,17 +73,10 @@ class TGLexer { /// by the SourceMgr object. int CurBuffer; - // IncludeDirectories - This is the list of directories we should search for - // include files in. - std::vector IncludeDirectories; public: TGLexer(SourceMgr &SrcMgr); ~TGLexer() {} - void setIncludeDirs(const std::vector &Dirs) { - IncludeDirectories = Dirs; - } - tgtok::TokKind Lex() { return CurCode = LexToken(); } diff --git a/utils/TableGen/TGParser.h b/utils/TableGen/TGParser.h index d06e9585cbb..9f4b6346064 100644 --- a/utils/TableGen/TGParser.h +++ b/utils/TableGen/TGParser.h @@ -49,8 +49,6 @@ class TGParser { public: TGParser(SourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {} - void setIncludeDirs(const std::vector &D){Lex.setIncludeDirs(D);} - /// ParseFile - Main entrypoint for parsing a tblgen file. These parser /// routines return true on error, or false on success. bool ParseFile(); diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index 0d5d86fdd6b..cb83cf3b4b8 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -146,12 +146,12 @@ static bool ParseFile(const std::string &Filename, // Tell SrcMgr about this buffer, which is what TGParser will pick up. SrcMgr.AddNewSourceBuffer(F, SMLoc()); - - TGParser Parser(SrcMgr); // Record the location of the include directory so that the lexer can find // it later. - Parser.setIncludeDirs(IncludeDirs); + SrcMgr.setIncludeDirs(IncludeDirs); + + TGParser Parser(SrcMgr); return Parser.ParseFile(); } -- 2.11.0