diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp index e50cf5b4a83440..499d9810677e63 100644 --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -267,7 +267,7 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, SmallVector, 4> ColRanges; std::pair LineAndCol; StringRef BufferID = ""; - std::string LineStr; + StringRef LineStr; if (Loc.isValid()) { unsigned CurBuf = FindBufferContainingLoc(Loc); @@ -288,7 +288,7 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const char *BufEnd = CurMB->getBufferEnd(); while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r') ++LineEnd; - LineStr = std::string(LineStart, LineEnd); + LineStr = StringRef(LineStart, LineEnd - LineStart); // Convert any ranges to column ranges that only intersect the line of the // location. @@ -370,8 +370,8 @@ SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, ArrayRef> Ranges, ArrayRef Hints) : SM(&sm), Loc(L), Filename(std::string(FN)), LineNo(Line), ColumnNo(Col), - Kind(Kind), Message(std::string(Msg)), LineContents(std::string(LineStr)), - Ranges(Ranges.vec()), FixIts(Hints.begin(), Hints.end()) { + Kind(Kind), Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()), + FixIts(Hints.begin(), Hints.end()) { llvm::sort(FixIts); } @@ -386,13 +386,12 @@ static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, size_t PrevHintEndCol = 0; - for (ArrayRef::iterator I = FixIts.begin(), E = FixIts.end(); I != E; - ++I) { + for (const llvm::SMFixIt &Fixit : FixIts) { // If the fixit contains a newline or tab, ignore it. - if (I->getText().find_first_of("\n\r\t") != StringRef::npos) + if (Fixit.getText().find_first_of("\n\r\t") != StringRef::npos) continue; - SMRange R = I->getRange(); + SMRange R = Fixit.getRange(); // If the line doesn't contain any part of the range, then ignore it. if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart) @@ -421,16 +420,15 @@ static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, // FIXME: This assertion is intended to catch unintended use of multibyte // characters in fixits. If we decide to do this, we'll have to track // separate byte widths for the source and fixit lines. - assert((size_t)sys::locale::columnWidth(I->getText()) == - I->getText().size()); + assert((size_t)sys::locale::columnWidth(Fixit.getText()) == + Fixit.getText().size()); // This relies on one byte per column in our fixit hints. - unsigned LastColumnModified = HintCol + I->getText().size(); + unsigned LastColumnModified = HintCol + Fixit.getText().size(); if (LastColumnModified > FixItLine.size()) FixItLine.resize(LastColumnModified, ' '); - std::copy(I->getText().begin(), I->getText().end(), - FixItLine.begin() + HintCol); + llvm::copy(Fixit.getText(), FixItLine.begin() + HintCol); PrevHintEndCol = LastColumnModified; @@ -534,11 +532,9 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, bool ShowColors, std::string CaretLine(NumColumns + 1, ' '); // Expand any ranges. - for (unsigned r = 0, e = Ranges.size(); r != e; ++r) { - std::pair R = Ranges[r]; + for (const std::pair &R : Ranges) std::fill(&CaretLine[R.first], &CaretLine[std::min((size_t)R.second, CaretLine.size())], '~'); - } // Add any fix-its. // FIXME: Find the beginning of the line properly for multibyte characters.