summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-07-16 06:14:39 +0000
committerChris Lattner <sabre@nondot.org>2009-07-16 06:14:39 +0000
commit8e25e2d801bb1119cea080c7c860adcfbf85d65d (patch)
tree0dd48770cc0b11e59fc5e5d79994070c76e82a90
parentc2b443a698493d3f2fe6ce2d8e7798a659eb3aae (diff)
downloadllvm-8e25e2d801bb1119cea080c7c860adcfbf85d65d.tar.gz
llvm-8e25e2d801bb1119cea080c7c860adcfbf85d65d.tar.bz2
llvm-8e25e2d801bb1119cea080c7c860adcfbf85d65d.tar.xz
implement .include in the lexer/parser instead of passing it into the streamer.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75896 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/MC/MCStreamer.h6
-rw-r--r--lib/MC/MCAsmStreamer.cpp6
-rw-r--r--test/MC/AsmParser/directive_include.s13
-rw-r--r--tools/llvm-mc/AsmLexer.cpp19
-rw-r--r--tools/llvm-mc/AsmLexer.h3
-rw-r--r--tools/llvm-mc/AsmParser.cpp20
6 files changed, 42 insertions, 25 deletions
diff --git a/include/llvm/MC/MCStreamer.h b/include/llvm/MC/MCStreamer.h
index 8daa246482..b8f1b2659d 100644
--- a/include/llvm/MC/MCStreamer.h
+++ b/include/llvm/MC/MCStreamer.h
@@ -160,12 +160,6 @@ namespace llvm {
/// @param AbortReason - The reason assembly is terminated, if non-NULL.
virtual void AbortAssembly(const char *AbortReason) = 0;
- /// SwitchInputAssemblyFile - Assemble the contents of the specified file in
- /// @param FileName at this point in the assembly.
- ///
- /// @param FileName - The file to assemble at this point
- virtual void SwitchInputAssemblyFile(const char *FileName) = 0;
-
/// DumpSymbolsandMacros - Dump to the specified file in @param FileName all
/// symbols and macros at this point in the assembly.
///
diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp
index 8ce3325c66..691694978d 100644
--- a/lib/MC/MCAsmStreamer.cpp
+++ b/lib/MC/MCAsmStreamer.cpp
@@ -57,8 +57,6 @@ namespace {
virtual void AbortAssembly(const char *AbortReason = NULL);
- virtual void SwitchInputAssemblyFile(const char *FileName);
-
virtual void DumpSymbolsandMacros(const char *FileName);
virtual void LoadSymbolsandMacros(const char *FileName);
@@ -143,10 +141,6 @@ void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
}
-void MCAsmStreamer::SwitchInputAssemblyFile(const char *FileName) {
- OS << ".include" << ' ' << FileName << '\n';
-}
-
void MCAsmStreamer::DumpSymbolsandMacros(const char *FileName) {
OS << ".dump" << ' ' << FileName << '\n';
}
diff --git a/test/MC/AsmParser/directive_include.s b/test/MC/AsmParser/directive_include.s
index 8c35d2a142..2721fee035 100644
--- a/test/MC/AsmParser/directive_include.s
+++ b/test/MC/AsmParser/directive_include.s
@@ -1,8 +1,9 @@
-# RUN: llvm-mc %s | FileCheck %s
+# RUN: llvm-mc %s -I %p | FileCheck %s
+# CHECK: TESTA:
# CHECK: TEST0:
-# CHECK: .include "some/include/file"
-# CHECK: .include "mary had a little lamb"
-TEST0:
- .include "some/include/file"
- .include "mary had a little lamb"
+# CHECK: .set a, 0
+# CHECK: TESTB:
+TESTA:
+ .include "directive_set.s"
+TESTB:
diff --git a/tools/llvm-mc/AsmLexer.cpp b/tools/llvm-mc/AsmLexer.cpp
index 7b744fbde6..6ee91455b7 100644
--- a/tools/llvm-mc/AsmLexer.cpp
+++ b/tools/llvm-mc/AsmLexer.cpp
@@ -54,6 +54,21 @@ asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
return asmtok::Error;
}
+/// EnterIncludeFile - Enter the specified file. This prints an error and
+/// returns true on failure.
+bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
+ int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
+ if (NewBuf == -1)
+ return true;
+
+ // Save the line number and lex buffer of the includer.
+ CurBuffer = NewBuf;
+ CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
+ CurPtr = CurBuf->getBufferStart();
+ return false;
+}
+
+
int AsmLexer::getNextChar() {
char CurChar = *CurPtr++;
switch (CurChar) {
@@ -72,6 +87,10 @@ int AsmLexer::getNextChar() {
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = ParentIncludeLoc.getPointer();
+
+ // Reset the token start pointer to the start of the new file.
+ TokStart = CurPtr;
+
return getNextChar();
}
diff --git a/tools/llvm-mc/AsmLexer.h b/tools/llvm-mc/AsmLexer.h
index 6360b1280c..5d59d0ad77 100644
--- a/tools/llvm-mc/AsmLexer.h
+++ b/tools/llvm-mc/AsmLexer.h
@@ -97,6 +97,9 @@ public:
SMLoc getLoc() const;
+ /// EnterIncludeFile - Enter the specified file. This returns true on failure.
+ bool EnterIncludeFile(const std::string &Filename);
+
void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
private:
diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp
index cb21a93bb1..68eb9d56c0 100644
--- a/tools/llvm-mc/AsmParser.cpp
+++ b/tools/llvm-mc/AsmParser.cpp
@@ -1168,21 +1168,27 @@ bool AsmParser::ParseDirectiveDarwinLsym() {
/// ParseDirectiveInclude
/// ::= .include "filename"
bool AsmParser::ParseDirectiveInclude() {
- const char *Str;
-
if (Lexer.isNot(asmtok::String))
return TokError("expected string in '.include' directive");
- Str = Lexer.getCurStrVal();
-
+ std::string Filename = Lexer.getCurStrVal();
+ SMLoc IncludeLoc = Lexer.getLoc();
Lexer.Lex();
if (Lexer.isNot(asmtok::EndOfStatement))
return TokError("unexpected token in '.include' directive");
- Lexer.Lex();
-
- Out.SwitchInputAssemblyFile(Str);
+ // Strip the quotes.
+ Filename = Filename.substr(1, Filename.size()-2);
+
+ // Attempt to switch the lexer to the included file before consuming the end
+ // of statement to avoid losing it when we switch.
+ if (Lexer.EnterIncludeFile(Filename)) {
+ Lexer.PrintMessage(IncludeLoc,
+ "Could not find include file '" + Filename + "'",
+ "error");
+ return true;
+ }
return false;
}