From d8b7aa26134d2abee777f745c32005e63dea2455 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 16 Oct 2011 04:47:35 +0000 Subject: Enhance llvm::SourceMgr to support diagnostic ranges, the same way clang does. Enhance the X86 asmparser to produce ranges in the one case that was annoying me, for example: test.s:10:15: error: invalid operand for instruction movl 0(%rax), 0(%edx) ^~~~~~~ It should be straight-forward to enhance filecheck, tblgen, and/or the .ll parser to use ranges where appropriate if someone is interested. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142106 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCParser/MCAsmParser.h | 11 ++++++++--- include/llvm/Support/SMLoc.h | 22 ++++++++++++++++++++-- include/llvm/Support/SourceMgr.h | 17 +++++++++-------- 3 files changed, 37 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/llvm/MC/MCParser/MCAsmParser.h b/include/llvm/MC/MCParser/MCAsmParser.h index 6ff175349e..039deaa070 100644 --- a/include/llvm/MC/MCParser/MCAsmParser.h +++ b/include/llvm/MC/MCParser/MCAsmParser.h @@ -11,6 +11,7 @@ #define LLVM_MC_MCASMPARSER_H #include "llvm/Support/DataTypes.h" +#include "llvm/ADT/ArrayRef.h" namespace llvm { class AsmToken; @@ -22,6 +23,7 @@ class MCExpr; class MCStreamer; class MCTargetAsmParser; class SMLoc; +class SMRange; class SourceMgr; class StringRef; class Twine; @@ -72,14 +74,16 @@ public: /// Msg. /// /// \return The return value is true, if warnings are fatal. - virtual bool Warning(SMLoc L, const Twine &Msg) = 0; + virtual bool Warning(SMLoc L, const Twine &Msg, + ArrayRef Ranges = ArrayRef()) = 0; /// Error - Emit an error at the location \arg L, with the message \arg /// Msg. /// /// \return The return value is always true, as an idiomatic convenience to /// clients. - virtual bool Error(SMLoc L, const Twine &Msg) = 0; + virtual bool Error(SMLoc L, const Twine &Msg, + ArrayRef Ranges = ArrayRef()) = 0; /// Lex - Get the next AsmToken in the stream, possibly handling file /// inclusion first. @@ -89,7 +93,8 @@ public: const AsmToken &getTok(); /// \brief Report an error at the current lexer location. - bool TokError(const Twine &Msg); + bool TokError(const Twine &Msg, + ArrayRef Ranges = ArrayRef()); /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) /// and set \arg Res to the identifier contents. diff --git a/include/llvm/Support/SMLoc.h b/include/llvm/Support/SMLoc.h index 02db32794b..d48bfcc30c 100644 --- a/include/llvm/Support/SMLoc.h +++ b/include/llvm/Support/SMLoc.h @@ -15,9 +15,11 @@ #ifndef SUPPORT_SMLOC_H #define SUPPORT_SMLOC_H +#include + namespace llvm { -// SMLoc - Represents a location in source code. +/// SMLoc - Represents a location in source code. class SMLoc { const char *Ptr; public: @@ -38,7 +40,23 @@ public: } }; -} +/// SMRange - Represents a range in source code. Note that unlike standard STL +/// ranges, the locations specified are considered to be *inclusive*. For +/// example, [X,X] *does* include X, it isn't an empty range. +class SMRange { +public: + SMLoc Start, End; + + SMRange() {} + SMRange(SMLoc Start, SMLoc End) : Start(Start), End(End) { + assert(Start.isValid() == End.isValid() && + "Start and end should either both be valid or both be invalid!"); + } + + bool isValid() const { return Start.isValid(); } +}; + +} // end namespace llvm #endif diff --git a/include/llvm/Support/SourceMgr.h b/include/llvm/Support/SourceMgr.h index 92ec891699..17d1df5aa7 100644 --- a/include/llvm/Support/SourceMgr.h +++ b/include/llvm/Support/SourceMgr.h @@ -17,10 +17,8 @@ #define SUPPORT_SOURCEMGR_H #include "llvm/Support/SMLoc.h" - +#include "llvm/ADT/ArrayRef.h" #include -#include -#include namespace llvm { class MemoryBuffer; @@ -125,6 +123,7 @@ public: /// prefixed to the message. /// @param ShowLine - Should the diagnostic show the source line. void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type, + ArrayRef Ranges = ArrayRef(), bool ShowLine = true) const; @@ -136,6 +135,7 @@ public: /// @param ShowLine - Should the diagnostic show the source line. SMDiagnostic GetMessage(SMLoc Loc, const Twine &Msg, const char *Type, + ArrayRef Ranges = ArrayRef(), bool ShowLine = true) const; /// PrintIncludeStack - Prints the names of included files and the line of the @@ -157,6 +157,7 @@ class SMDiagnostic { int LineNo, ColumnNo; std::string Message, LineContents; unsigned ShowLine : 1; + std::vector > Ranges; public: // Null diagnostic. @@ -170,9 +171,7 @@ public: SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN, int Line, int Col, const std::string &Msg, const std::string &LineStr, - bool showline = true) - : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg), - LineContents(LineStr), ShowLine(showline) {} + ArrayRef > Ranges, bool showline); const SourceMgr *getSourceMgr() const { return SM; } SMLoc getLoc() const { return Loc; } @@ -182,8 +181,10 @@ public: const std::string &getMessage() const { return Message; } const std::string &getLineContents() const { return LineContents; } bool getShowLine() const { return ShowLine; } - - void Print(const char *ProgName, raw_ostream &S) const; + const std::vector > &getRanges() const { + return Ranges; + } + void print(const char *ProgName, raw_ostream &S) const; }; } // end llvm namespace -- cgit v1.2.3