From dd137903e47fdb5822724baaddae88f119badc86 Mon Sep 17 00:00:00 2001 From: Joerg Sonnenberger Date: Wed, 1 Jun 2011 13:10:15 +0000 Subject: Add new -d option to tblgen. It writes a make(1)-style dependency file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132395 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/TGLexer.cpp | 5 +++- utils/TableGen/TGLexer.h | 7 +++++ utils/TableGen/TGParser.h | 3 ++ utils/TableGen/TableGen.cpp | 72 +++++++++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 30 deletions(-) (limited to 'utils') diff --git a/utils/TableGen/TGLexer.cpp b/utils/TableGen/TGLexer.cpp index 82d2b6491a..572c36dcca 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/utils/TableGen/TGLexer.cpp @@ -267,14 +267,17 @@ bool TGLexer::LexInclude() { // Get the string. std::string Filename = CurStrVal; + std::string IncludedFile; - CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr)); + CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr), + IncludedFile); if (CurBuffer == -1) { PrintError(getLoc(), "Could not find include file '" + Filename + "'"); return true; } + Dependencies.push_back(IncludedFile); // Save the line number and lex buffer of the includer. CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); CurPtr = CurBuf->getBufferStart(); diff --git a/utils/TableGen/TGLexer.h b/utils/TableGen/TGLexer.h index e1aa5a7232..c2a64532fe 100644 --- a/utils/TableGen/TGLexer.h +++ b/utils/TableGen/TGLexer.h @@ -16,6 +16,7 @@ #include "llvm/Support/DataTypes.h" #include +#include #include namespace llvm { @@ -71,6 +72,8 @@ class TGLexer { /// CurBuffer - This is the current buffer index we're lexing from as managed /// by the SourceMgr object. int CurBuffer; + /// Dependencies - This is the list of all included files. + std::vector Dependencies; public: TGLexer(SourceMgr &SrcMgr); @@ -79,6 +82,10 @@ public: tgtok::TokKind Lex() { return CurCode = LexToken(); } + + const std::vector &getDependencies() const { + return Dependencies; + } tgtok::TokKind getCode() const { return CurCode; } diff --git a/utils/TableGen/TGParser.h b/utils/TableGen/TGParser.h index 9cdf68ff97..419a99b13f 100644 --- a/utils/TableGen/TGParser.h +++ b/utils/TableGen/TGParser.h @@ -66,6 +66,9 @@ public: bool TokError(const Twine &Msg) const { return Error(Lex.getLoc(), Msg); } + const std::vector &getDependencies() const { + return Lex.getDependencies(); + } private: // Semantic analysis methods. bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV); bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index d88a2d6a0e..fb941c44dc 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -172,6 +172,10 @@ namespace { OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-")); + cl::opt + DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"), + cl::init("")); + cl::opt InputFilename(cl::Positional, cl::desc(""), cl::init("-")); @@ -192,34 +196,6 @@ void llvm::PrintError(SMLoc ErrorLoc, const Twine &Msg) { SrcMgr.PrintMessage(ErrorLoc, Msg, "error"); } - - -/// ParseFile - this function begins the parsing of the specified tablegen -/// file. -static bool ParseFile(const std::string &Filename, - const std::vector &IncludeDirs, - SourceMgr &SrcMgr, - RecordKeeper &Records) { - OwningPtr File; - if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) { - errs() << "Could not open input file '" << Filename << "': " - << ec.message() <<"\n"; - return true; - } - MemoryBuffer *F = File.take(); - - // Tell SrcMgr about this buffer, which is what TGParser will pick up. - SrcMgr.AddNewSourceBuffer(F, SMLoc()); - - // Record the location of the include directory so that the lexer can find - // it later. - SrcMgr.setIncludeDirs(IncludeDirs); - - TGParser Parser(SrcMgr, Records); - - return Parser.ParseFile(); -} - int main(int argc, char **argv) { RecordKeeper Records; @@ -230,7 +206,24 @@ int main(int argc, char **argv) { try { // Parse the input file. - if (ParseFile(InputFilename, IncludeDirs, SrcMgr, Records)) + OwningPtr File; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) { + errs() << "Could not open input file '" << InputFilename << "': " + << ec.message() <<"\n"; + return 1; + } + MemoryBuffer *F = File.take(); + + // Tell SrcMgr about this buffer, which is what TGParser will pick up. + SrcMgr.AddNewSourceBuffer(F, SMLoc()); + + // Record the location of the include directory so that the lexer can find + // it later. + SrcMgr.setIncludeDirs(IncludeDirs); + + TGParser Parser(SrcMgr, Records); + + if (Parser.ParseFile()) return 1; std::string Error; @@ -240,6 +233,27 @@ int main(int argc, char **argv) { << ":" << Error << "\n"; return 1; } + if (!DependFilename.empty()) { + if (OutputFilename == "-") { + errs() << argv[0] << ": the option -d must be used together with -o\n"; + return 1; + } + tool_output_file DepOut(DependFilename.c_str(), Error); + if (!Error.empty()) { + errs() << argv[0] << ": error opening " << DependFilename + << ":" << Error << "\n"; + return 1; + } + DepOut.os() << DependFilename << ":"; + const std::vector &Dependencies = Parser.getDependencies(); + for (std::vector::const_iterator I = Dependencies.begin(), + E = Dependencies.end(); + I != E; ++I) { + DepOut.os() << " " << (*I); + } + DepOut.os() << "\n"; + DepOut.keep(); + } switch (Action) { case PrintRecords: -- cgit v1.2.3