diff --git a/clang/include/clang/Basic/FileManager.h b/clang/include/clang/Basic/FileManager.h --- a/clang/include/clang/Basic/FileManager.h +++ b/clang/include/clang/Basic/FileManager.h @@ -18,6 +18,7 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" diff --git a/llvm/include/llvm/Support/SourceMgr.h b/llvm/include/llvm/Support/SourceMgr.h --- a/llvm/include/llvm/Support/SourceMgr.h +++ b/llvm/include/llvm/Support/SourceMgr.h @@ -15,19 +15,9 @@ #ifndef LLVM_SUPPORT_SOURCEMGR_H #define LLVM_SUPPORT_SOURCEMGR_H -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/None.h" -#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SMLoc.h" -#include -#include -#include -#include -#include #include namespace llvm { @@ -57,21 +47,17 @@ /// The memory buffer for the file. std::unique_ptr Buffer; - /// Helper type for OffsetCache below: since we're storing many offsets - /// into relatively small files (often smaller than 2^8 or 2^16 bytes), - /// we select the offset vector element type dynamically based on the - /// size of Buffer. - using VariableSizeOffsets = - PointerUnion *, std::vector *, - std::vector *, std::vector *>; - /// Vector of offsets into Buffer at which there are line-endings /// (lazily populated). Once populated, the '\n' that marks the end of /// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since /// these offsets are in sorted (ascending) order, they can be /// binary-searched for the first one after any given offset (eg. an /// offset corresponding to a particular SMLoc). - mutable VariableSizeOffsets OffsetCache; + /// + /// Since we're storing offsets into relatively small files (often smaller + /// than 2^8 or 2^16 bytes), we select the offset vector element type + /// dynamically based on the size of Buffer. + mutable void *OffsetCache = nullptr; /// Look up a given \p Ptr in in the buffer, determining which line it came /// from. @@ -196,14 +182,14 @@ /// \param ShowColors Display colored messages if output is a terminal and /// the default error handler is used. void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, - ArrayRef Ranges = None, - ArrayRef FixIts = None, + ArrayRef Ranges = {}, + ArrayRef FixIts = {}, bool ShowColors = true) const; /// Emits a diagnostic to llvm::errs(). void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, - ArrayRef Ranges = None, - ArrayRef FixIts = None, + ArrayRef Ranges = {}, + ArrayRef FixIts = {}, bool ShowColors = true) const; /// Emits a manually-constructed diagnostic to the given output stream. @@ -219,8 +205,8 @@ /// \param Msg If non-null, the kind of message (e.g., "error") which is /// prefixed to the message. SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, - ArrayRef Ranges = None, - ArrayRef FixIts = None) const; + ArrayRef Ranges = {}, + ArrayRef FixIts = {}) const; /// Prints the names of included files and the line of the file they were /// included from. A diagnostic handler can use this before printing its @@ -238,17 +224,10 @@ std::string Text; public: - // FIXME: Twine.str() is not very efficient. - SMFixIt(SMLoc Loc, const Twine &Insertion) - : Range(Loc, Loc), Text(Insertion.str()) { - assert(Loc.isValid()); - } + SMFixIt(SMRange R, const Twine &Replacement); - // FIXME: Twine.str() is not very efficient. - SMFixIt(SMRange R, const Twine &Replacement) - : Range(R), Text(Replacement.str()) { - assert(R.isValid()); - } + SMFixIt(SMLoc Loc, const Twine &Replacement) + : SMFixIt(SMRange(Loc, Loc), Replacement) {} StringRef getText() const { return Text; } SMRange getRange() const { return Range; } @@ -286,7 +265,7 @@ SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, int Col, SourceMgr::DiagKind Kind, StringRef Msg, StringRef LineStr, ArrayRef> Ranges, - ArrayRef FixIts = None); + ArrayRef FixIts = {}); const SourceMgr *getSourceMgr() const { return SM; } SMLoc getLoc() const { return Loc; } diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -69,16 +69,13 @@ } template -static std::vector &GetOrCreateOffsetCache( - PointerUnion *, std::vector *, - std::vector *, std::vector *> &OffsetCache, - MemoryBuffer *Buffer) { - if (!OffsetCache.isNull()) - return *OffsetCache.get *>(); +static std::vector &GetOrCreateOffsetCache(void *&OffsetCache, + MemoryBuffer *Buffer) { + if (OffsetCache) + return *static_cast *>(OffsetCache); // Lazily fill in the offset cache. auto *Offsets = new std::vector(); - OffsetCache = Offsets; size_t Sz = Buffer->getBufferSize(); assert(Sz <= std::numeric_limits::max()); StringRef S = Buffer->getBuffer(); @@ -87,6 +84,7 @@ Offsets->push_back(static_cast(N)); } + OffsetCache = Offsets; return *Offsets; } @@ -164,15 +162,16 @@ } SourceMgr::SrcBuffer::~SrcBuffer() { - if (!OffsetCache.isNull()) { - if (OffsetCache.is *>()) - delete OffsetCache.get *>(); - else if (OffsetCache.is *>()) - delete OffsetCache.get *>(); - else if (OffsetCache.is *>()) - delete OffsetCache.get *>(); + if (OffsetCache) { + size_t Sz = Buffer->getBufferSize(); + if (Sz <= std::numeric_limits::max()) + delete static_cast *>(OffsetCache); + else if (Sz <= std::numeric_limits::max()) + delete static_cast *>(OffsetCache); + else if (Sz <= std::numeric_limits::max()) + delete static_cast *>(OffsetCache); else - delete OffsetCache.get *>(); + delete static_cast *>(OffsetCache); OffsetCache = nullptr; } } @@ -328,6 +327,15 @@ PrintMessage(errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors); } +//===----------------------------------------------------------------------===// +// SMFixIt Implementation +//===----------------------------------------------------------------------===// + +SMFixIt::SMFixIt(SMRange R, const Twine &Replacement) + : Range(R), Text(Replacement.str()) { + assert(R.isValid()); +} + //===----------------------------------------------------------------------===// // SMDiagnostic Implementation //===----------------------------------------------------------------------===//