[llvm-commits] [llvm] r74735 - in /llvm/trunk: include/llvm/Support/SourceMgr.h lib/AsmParser/LLLexer.cpp lib/AsmParser/LLLexer.h lib/AsmParser/LLParser.cpp lib/AsmParser/LLParser.h lib/AsmParser/Parser.cpp lib/Support/SourceMgr.cpp
Chris Lattner
sabre at nondot.org
Thu Jul 2 16:08:14 PDT 2009
Author: lattner
Date: Thu Jul 2 18:08:13 2009
New Revision: 74735
URL: http://llvm.org/viewvc/llvm-project?rev=74735&view=rev
Log:
switch the .ll parser to use SourceMgr.
Modified:
llvm/trunk/include/llvm/Support/SourceMgr.h
llvm/trunk/lib/AsmParser/LLLexer.cpp
llvm/trunk/lib/AsmParser/LLLexer.h
llvm/trunk/lib/AsmParser/LLParser.cpp
llvm/trunk/lib/AsmParser/LLParser.h
llvm/trunk/lib/AsmParser/Parser.cpp
llvm/trunk/lib/Support/SourceMgr.cpp
Modified: llvm/trunk/include/llvm/Support/SourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SourceMgr.h?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SourceMgr.h (original)
+++ llvm/trunk/include/llvm/Support/SourceMgr.h Thu Jul 2 18:08:13 2009
@@ -118,6 +118,16 @@
/// prefixed to the message.
void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
+
+ /// GetMessage - Return an SMDiagnostic at the specified location with the
+ /// specified string.
+ ///
+ /// @param Type - If non-null, the kind of message (e.g., "error") which is
+ /// prefixed to the message.
+ SMDiagnostic GetMessage(SMLoc Loc,
+ const std::string &Msg, const char *Type) const;
+
+
private:
void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
};
Modified: llvm/trunk/lib/AsmParser/LLLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.cpp?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLLexer.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLLexer.cpp Thu Jul 2 18:08:13 2009
@@ -24,24 +24,7 @@
using namespace llvm;
bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
- // Scan backward to find the start of the line.
- const char *LineStart = ErrorLoc;
- while (LineStart != CurBuf->getBufferStart() &&
- LineStart[-1] != '\n' && LineStart[-1] != '\r')
- --LineStart;
- // Get the end of the line.
- const char *LineEnd = ErrorLoc;
- while (LineEnd != CurBuf->getBufferEnd() &&
- LineEnd[0] != '\n' && LineEnd[0] != '\r')
- ++LineEnd;
-
- unsigned LineNo = 1;
- for (const char *FP = CurBuf->getBufferStart(); FP != ErrorLoc; ++FP)
- if (*FP == '\n') ++LineNo;
-
- ErrorInfo = SMDiagnostic(CurBuf->getBufferIdentifier(),
- LineNo, ErrorLoc-LineStart, Msg,
- std::string(LineStart, LineEnd));
+ ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
return true;
}
@@ -197,8 +180,8 @@
// Lexer definition.
//===----------------------------------------------------------------------===//
-LLLexer::LLLexer(MemoryBuffer *StartBuf, SMDiagnostic &Err)
- : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
+LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err)
+ : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), APFloatVal(0.0) {
CurPtr = CurBuf->getBufferStart();
}
Modified: llvm/trunk/lib/AsmParser/LLLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.h?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLLexer.h (original)
+++ llvm/trunk/lib/AsmParser/LLLexer.h Thu Jul 2 18:08:13 2009
@@ -17,6 +17,7 @@
#include "LLToken.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/APFloat.h"
+#include "llvm/Support/SourceMgr.h"
#include <string>
namespace llvm {
@@ -28,6 +29,7 @@
const char *CurPtr;
MemoryBuffer *CurBuf;
SMDiagnostic &ErrorInfo;
+ SourceMgr &SM;
// Information about the current token.
const char *TokStart;
@@ -40,15 +42,15 @@
std::string TheError;
public:
- explicit LLLexer(MemoryBuffer *StartBuf, SMDiagnostic &);
+ explicit LLLexer(MemoryBuffer *StartBuf, SourceMgr &SM, SMDiagnostic &);
~LLLexer() {}
lltok::Kind Lex() {
return CurKind = LexToken();
}
- typedef const char* LocTy;
- LocTy getLoc() const { return TokStart; }
+ typedef SMLoc LocTy;
+ LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
lltok::Kind getKind() const { return CurKind; }
const std::string getStrVal() const { return StrVal; }
const Type *getTyVal() const { return TyVal; }
@@ -58,7 +60,7 @@
bool Error(LocTy L, const std::string &Msg) const;
- bool Error(const std::string &Msg) const { return Error(CurPtr, Msg); }
+ bool Error(const std::string &Msg) const { return Error(getLoc(), Msg); }
std::string getFilename() const;
private:
Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Thu Jul 2 18:08:13 2009
@@ -131,7 +131,7 @@
unsigned Linkage, Visibility;
if (ParseOptionalLinkage(Linkage) ||
ParseOptionalVisibility(Visibility) ||
- ParseGlobal("", 0, Linkage, true, Visibility))
+ ParseGlobal("", SMLoc(), Linkage, true, Visibility))
return true;
break;
}
@@ -140,7 +140,7 @@
case lltok::kw_protected: { // OptionalVisibility
unsigned Visibility;
if (ParseOptionalVisibility(Visibility) ||
- ParseGlobal("", 0, 0, false, Visibility))
+ ParseGlobal("", SMLoc(), 0, false, Visibility))
return true;
break;
}
@@ -149,7 +149,7 @@
case lltok::kw_addrspace: // OptionalAddrSpace
case lltok::kw_constant: // GlobalType
case lltok::kw_global: // GlobalType
- if (ParseGlobal("", 0, 0, false, 0)) return true;
+ if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
break;
}
}
@@ -3162,7 +3162,7 @@
unsigned Opc) {
PATypeHolder Ty(Type::VoidTy);
Value *Size = 0;
- LocTy SizeLoc = 0;
+ LocTy SizeLoc;
unsigned Alignment = 0;
if (ParseType(Ty)) return true;
Modified: llvm/trunk/lib/AsmParser/LLParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.h?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.h (original)
+++ llvm/trunk/lib/AsmParser/LLParser.h Thu Jul 2 18:08:13 2009
@@ -73,8 +73,8 @@
std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
std::vector<GlobalValue*> NumberedVals;
public:
- LLParser(MemoryBuffer *F, SMDiagnostic &Err, Module *m) :
- Context(m->getContext()), Lex(F, Err), M(m) {}
+ LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
+ Context(m->getContext()), Lex(F, SM, Err), M(m) {}
bool Run();
LLVMContext& getContext() { return Context; }
Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Thu Jul 2 18:08:13 2009
@@ -24,33 +24,38 @@
Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
LLVMContext &Context) {
std::string ErrorStr;
- OwningPtr<MemoryBuffer>
- F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
+ MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
if (F == 0) {
Err = SMDiagnostic("", -1, -1,
"Could not open input file '" + Filename + "'", "");
return 0;
}
+ SourceMgr SM;
+ SM.AddNewSourceBuffer(F, SMLoc());
+
OwningPtr<Module> M(new Module(Filename, Context));
- if (LLParser(F.get(), Err, M.get()).Run())
+ if (LLParser(F, SM, Err, M.get()).Run())
return 0;
return M.take();
}
Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
SMDiagnostic &Err, LLVMContext &Context) {
- OwningPtr<MemoryBuffer>
- F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
- "<string>"));
+ MemoryBuffer *F =
+ MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
+ "<string>");
+ SourceMgr SM;
+ SM.AddNewSourceBuffer(F, SMLoc());
+
// If we are parsing into an existing module, do it.
if (M)
- return LLParser(F.get(), Err, M).Run() ? 0 : M;
+ return LLParser(F, SM, Err, M).Run() ? 0 : M;
// Otherwise create a new module.
OwningPtr<Module> M2(new Module("<string>", Context));
- if (LLParser(F.get(), Err, M2.get()).Run())
+ if (LLParser(F, SM, Err, M2.get()).Run())
return 0;
return M2.take();
}
Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=74735&r1=74734&r2=74735&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Thu Jul 2 18:08:13 2009
@@ -90,17 +90,19 @@
}
-void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
- const char *Type) const {
- raw_ostream &OS = errs();
+/// GetMessage - Return an SMDiagnostic at the specified location with the
+/// specified string.
+///
+/// @param Type - If non-null, the kind of message (e.g., "error") which is
+/// prefixed to the message.
+SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const std::string &Msg,
+ const char *Type) const {
// First thing to do: find the current buffer containing the specified
// location.
int CurBuf = FindBufferContainingLoc(Loc);
assert(CurBuf != -1 && "Invalid or unspecified location!");
- PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
-
MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
@@ -122,12 +124,21 @@
}
PrintedMsg += Msg;
-
// Print out the line.
- SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
- Loc.getPointer()-LineStart, PrintedMsg,
- std::string(LineStart, LineEnd)).Print(0, OS);
-
+ return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
+ Loc.getPointer()-LineStart, PrintedMsg,
+ std::string(LineStart, LineEnd));
+}
+
+void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
+ const char *Type) const {
+ raw_ostream &OS = errs();
+
+ int CurBuf = FindBufferContainingLoc(Loc);
+ assert(CurBuf != -1 && "Invalid or unspecified location!");
+ PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
+
+ GetMessage(Loc, Msg, Type).Print(0, OS);
}
//===----------------------------------------------------------------------===//
More information about the llvm-commits
mailing list