diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index 7ad26d64e7379..47a719c54e15a 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -131,11 +131,6 @@ class SourceManager { /// buffer identifier, or None if there is no such buffer. Optional getIDForBufferIdentifier(StringRef BufIdentifier); - /// Returns the identifier for the buffer with the given ID. - /// - /// \p BufferID must be a valid buffer ID. - StringRef getIdentifierForBuffer(unsigned BufferID) const; - /// \brief Returns a SourceRange covering the entire specified buffer. /// /// Note that the start location might not point at the first token: it @@ -163,40 +158,71 @@ class SourceManager { return getLocForBufferStart(BufferID).getAdvancedLoc(Offset); } - /// Returns the identifier string for the buffer containing the given source + /// Returns the identifier string for the buffer with the given ID. + /// + /// \p BufferID must be a valid buffer ID. + StringRef getIdentifierForBuffer(unsigned BufferID) const; + + /// Returns the identifier string for the buffer containing the given valid + /// source location. + /// + /// Note that this doesn't respect #sourceLocation directives. + StringRef getIdentifierForBuffer(SourceLoc Loc) const { + return getIdentifierForBuffer(findBufferContainingLoc(Loc)); + } + + /// Returns the physical line and column represented by the given valid + /// source location. + /// + /// Note that this doesn't respect #sourceLocation directives. + /// If \p BufferID is provided, \p Loc must come from that source buffer. + std::pair + getLineAndColumnInBuffer(SourceLoc Loc, unsigned BufferID = 0) const { + return LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID); + } + + /// Translate line and column pair to the offset. + llvm::Optional + getOffsetForLineAndColumnInBuffer(unsigned BufferId, unsigned Line, + unsigned Col) const; + + + /// Returns the source location from the given physical line and col in the + /// specified buffer. + SourceLoc getLocForLineAndColumnInBuffer(unsigned BufferId, unsigned Line, + unsigned Col) const { + auto Offset = getOffsetForLineAndColumnInBuffer(BufferId, Line, Col); + return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) : + SourceLoc(); + } + + /// Returns the presumed filename string containing the given valid source /// location. /// - /// This respects #line directives. - StringRef getBufferIdentifierForLoc(SourceLoc Loc) const { + /// This respects \c #sourceLocation direvtive. + StringRef getPresumedFilenameForLoc(SourceLoc Loc) const { if (auto VFile = getVirtualFile(Loc)) return VFile->Name; else - return getIdentifierForBuffer(findBufferContainingLoc(Loc)); + return getIdentifierForBuffer(Loc); } - /// Returns the line and column represented by the given source location. + /// Returns the line and column represented by the given valid source + /// location. /// /// If \p BufferID is provided, \p Loc must come from that source buffer. - /// - /// This respects #line directives. + /// This respects \c #sourceLocation direvtive. std::pair - getLineAndColumn(SourceLoc Loc, unsigned BufferID = 0) const { + getPresumedLineAndColumnForLoc(SourceLoc Loc, unsigned BufferID = 0) const { assert(Loc.isValid()); - int LineOffset = getLineOffset(Loc); int l, c; std::tie(l, c) = LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID); - assert(LineOffset+l > 0 && "bogus line offset"); - return { LineOffset + l, c }; - } - /// Returns the real line number for a source location. - /// - /// If \p BufferID is provided, \p Loc must come from that source buffer. - /// - /// This does not respect #line directives. - unsigned getLineNumber(SourceLoc Loc, unsigned BufferID = 0) const { - assert(Loc.isValid()); - return LLVMSourceMgr.FindLineNumber(Loc.Value, BufferID); + if (auto VFile = getVirtualFile(Loc)) { + assert(l + VFile->LineOffset > 0 && "bogus line offset"); + l += VFile->LineOffset; + } + return { l, c }; } StringRef extractText(CharSourceRange Range, @@ -210,25 +236,8 @@ class SourceManager { /// Verifies that all buffers are still valid. void verifyAllBuffers() const; - /// Translate line and column pair to the offset. - llvm::Optional resolveFromLineCol(unsigned BufferId, unsigned Line, - unsigned Col) const; - - SourceLoc getLocForLineCol(unsigned BufferId, unsigned Line, unsigned Col) const { - auto Offset = resolveFromLineCol(BufferId, Line, Col); - return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) : - SourceLoc(); - } - private: const VirtualFile *getVirtualFile(SourceLoc Loc) const; - - int getLineOffset(SourceLoc Loc) const { - if (auto VFile = getVirtualFile(Loc)) - return VFile->LineOffset; - else - return 0; - } }; } // end namespace swift diff --git a/include/swift/Parse/Lexer.h b/include/swift/Parse/Lexer.h index 5c843d376e1a4..10cd85b9ac861 100644 --- a/include/swift/Parse/Lexer.h +++ b/include/swift/Parse/Lexer.h @@ -338,8 +338,6 @@ class Lexer { static SourceLoc getLocForStartOfToken(SourceManager &SM, unsigned BufferID, unsigned Offset); - static SourceLoc getLocForStartOfToken(SourceManager &SM, SourceLoc Loc); - /// Retrieve the start location of the line containing the given location. /// the given location. static SourceLoc getLocForStartOfLine(SourceManager &SM, SourceLoc Loc); diff --git a/lib/AST/ASTScope.cpp b/lib/AST/ASTScope.cpp index a2fc5f7e70591..211849c608855 100644 --- a/lib/AST/ASTScope.cpp +++ b/lib/AST/ASTScope.cpp @@ -1973,8 +1973,8 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level, return; } - auto startLineAndCol = sourceMgr.getLineAndColumn(range.Start); - auto endLineAndCol = sourceMgr.getLineAndColumn(range.End); + auto startLineAndCol = sourceMgr.getPresumedLineAndColumnForLoc(range.Start); + auto endLineAndCol = sourceMgr.getPresumedLineAndColumnForLoc(range.End); out << " [" << startLineAndCol.first << ":" << startLineAndCol.second << " - " << endLineAndCol.first << ":" << endLineAndCol.second << "]"; diff --git a/lib/AST/DeclContext.cpp b/lib/AST/DeclContext.cpp index da16e4353f6ac..35b51376bb2e5 100644 --- a/lib/AST/DeclContext.cpp +++ b/lib/AST/DeclContext.cpp @@ -651,7 +651,7 @@ static unsigned getLineNumber(DCType *DC) { return 0; const ASTContext &ctx = static_cast(DC)->getASTContext(); - return ctx.SourceMgr.getLineAndColumn(loc).first; + return ctx.SourceMgr.getPresumedLineAndColumnForLoc(loc).first; } bool DeclContext::classof(const Decl *D) { diff --git a/lib/AST/GenericSignatureBuilder.cpp b/lib/AST/GenericSignatureBuilder.cpp index cc9fbfb1030ec..f41bee6de9c1c 100644 --- a/lib/AST/GenericSignatureBuilder.cpp +++ b/lib/AST/GenericSignatureBuilder.cpp @@ -596,7 +596,7 @@ void RequirementSource::print(llvm::raw_ostream &out, unsigned bufferID = srcMgr->findBufferContainingLoc(loc); - auto lineAndCol = srcMgr->getLineAndColumn(loc, bufferID); + auto lineAndCol = srcMgr->getPresumedLineAndColumnForLoc(loc, bufferID); out << " @ " << lineAndCol.first << ':' << lineAndCol.second; }; diff --git a/lib/AST/RawComment.cpp b/lib/AST/RawComment.cpp index 2925f81589896..be54578866234 100644 --- a/lib/AST/RawComment.cpp +++ b/lib/AST/RawComment.cpp @@ -58,10 +58,10 @@ SingleRawComment::SingleRawComment(CharSourceRange Range, const SourceManager &SourceMgr) : Range(Range), RawText(SourceMgr.extractText(Range)), Kind(static_cast(getCommentKind(RawText))), - EndLine(SourceMgr.getLineNumber(Range.getEnd())) { - auto StartLineAndColumn = SourceMgr.getLineAndColumn(Range.getStart()); - StartLine = StartLineAndColumn.first; - StartColumn = StartLineAndColumn.second; + EndLine(SourceMgr.getLineAndColumnInBuffer(Range.getEnd()).first) { + auto Start = SourceMgr.getLineAndColumnInBuffer(Range.getStart()); + StartLine = Start.first; + StartColumn = Start.second; } SingleRawComment::SingleRawComment(StringRef RawText, unsigned StartColumn) diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index be77b46955fb7..824feb88e7528 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -208,7 +208,7 @@ void SourceLoc::printLineAndColumn(raw_ostream &OS, return; } - auto LineAndCol = SM.getLineAndColumn(*this); + auto LineAndCol = SM.getPresumedLineAndColumnForLoc(*this); OS << "line:" << LineAndCol.first << ':' << LineAndCol.second; } @@ -227,7 +227,7 @@ void SourceLoc::print(raw_ostream &OS, const SourceManager &SM, OS << "line"; } - auto LineAndCol = SM.getLineAndColumn(*this, BufferID); + auto LineAndCol = SM.getPresumedLineAndColumnForLoc(*this, BufferID); OS << ':' << LineAndCol.first << ':' << LineAndCol.second; } @@ -281,9 +281,9 @@ void CharSourceRange::dump(const SourceManager &SM) const { print(llvm::errs(), SM); } -llvm::Optional SourceManager::resolveFromLineCol(unsigned BufferId, - unsigned Line, - unsigned Col) const { +llvm::Optional +SourceManager::getOffsetForLineAndColumnInBuffer(unsigned BufferId, unsigned Line, + unsigned Col) const { if (Line == 0 || Col == 0) { return None; } diff --git a/lib/Frontend/DiagnosticVerifier.cpp b/lib/Frontend/DiagnosticVerifier.cpp index 3e8d08bbc80ea..ec7025b421581 100644 --- a/lib/Frontend/DiagnosticVerifier.cpp +++ b/lib/Frontend/DiagnosticVerifier.cpp @@ -323,7 +323,7 @@ bool DiagnosticVerifier::verifyFile(unsigned BufferID, if (PrevExpectedContinuationLine) Expected.LineNo = PrevExpectedContinuationLine; else - Expected.LineNo = SM.getLineAndColumn( + Expected.LineNo = SM.getPresumedLineAndColumnForLoc( BufferStartLoc.getAdvancedLoc(MatchStart.data() - InputFile.data()), BufferID).first; Expected.LineNo += LineOffset; diff --git a/lib/Frontend/PrintingDiagnosticConsumer.cpp b/lib/Frontend/PrintingDiagnosticConsumer.cpp index 7a95c5584ade6..02a6d62bf7144 100644 --- a/lib/Frontend/PrintingDiagnosticConsumer.cpp +++ b/lib/Frontend/PrintingDiagnosticConsumer.cpp @@ -121,7 +121,7 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind, std::string LineStr; if (Loc.isValid()) { - BufferID = getBufferIdentifierForLoc(Loc); + BufferID = getPresumedFilenameForLoc(Loc); auto CurMB = LLVMSourceMgr.getMemoryBuffer(findBufferContainingLoc(Loc)); // Scan backward to find the start of the line. @@ -160,7 +160,7 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind, R.End.getPointer()-LineStart)); } - LineAndCol = getLineAndColumn(Loc); + LineAndCol = getPresumedLineAndColumnForLoc(Loc); } return llvm::SMDiagnostic(LLVMSourceMgr, Loc.Value, BufferID, diff --git a/lib/Frontend/SerializedDiagnosticConsumer.cpp b/lib/Frontend/SerializedDiagnosticConsumer.cpp index fe00841f1579a..bc7a67a5fe7f0 100644 --- a/lib/Frontend/SerializedDiagnosticConsumer.cpp +++ b/lib/Frontend/SerializedDiagnosticConsumer.cpp @@ -239,7 +239,7 @@ void SerializedDiagnosticConsumer::addLocToRecord(SourceLoc Loc, } unsigned line, col; - std::tie(line, col) = SM.getLineAndColumn(Loc); + std::tie(line, col) = SM.getPresumedLineAndColumnForLoc(Loc); Record.push_back(getEmitFile(Filename)); Record.push_back(line); diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp index 699c6ed337313..96a963ca33d21 100644 --- a/lib/FrontendTool/FrontendTool.cpp +++ b/lib/FrontendTool/FrontendTool.cpp @@ -443,9 +443,8 @@ static bool performCompile(std::unique_ptr &Instance, SourceManager &sourceMgr = Instance->getSourceMgr(); // Probe each of the locations, and dump what we find. for (auto lineColumn : opts.DumpScopeMapLocations) { - SourceLoc loc = sourceMgr.getLocForLineCol(*bufferID, - lineColumn.first, - lineColumn.second); + SourceLoc loc = sourceMgr.getLocForLineAndColumnInBuffer( + *bufferID, lineColumn.first, lineColumn.second); if (loc.isInvalid()) continue; llvm::errs() << "***Scope at " << lineColumn.first << ":" diff --git a/lib/IDE/CommentConversion.cpp b/lib/IDE/CommentConversion.cpp index 4ed59941de158..fed2292744a6a 100644 --- a/lib/IDE/CommentConversion.cpp +++ b/lib/IDE/CommentConversion.cpp @@ -302,7 +302,7 @@ void CommentToXMLConverter::visitDocComment(const DocComment *DC) { const auto &SM = D->getASTContext().SourceMgr; unsigned BufferID = SM.findBufferContainingLoc(Loc); StringRef FileName = SM.getIdentifierForBuffer(BufferID); - auto LineAndColumn = SM.getLineAndColumn(Loc); + auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc); OS << " file=\""; appendWithXMLEscaping(OS, FileName); OS << "\""; diff --git a/lib/IDE/Formatting.cpp b/lib/IDE/Formatting.cpp index 6a731445e3eba..44059c8107fd9 100644 --- a/lib/IDE/Formatting.cpp +++ b/lib/IDE/Formatting.cpp @@ -118,7 +118,7 @@ class FormatContext { if (Stmt *S = Cursor->getAsStmt()) { SourceLoc SL = S->getStartLoc(); - return SM.getLineAndColumn(SL); + return SM.getPresumedLineAndColumnForLoc(SL); } if (Decl *D = Cursor->getAsDecl()) { SourceLoc SL = D->getStartLoc(); @@ -131,11 +131,11 @@ class FormatContext { SL = AttrLoc; } - return SM.getLineAndColumn(SL); + return SM.getPresumedLineAndColumnForLoc(SL); } if (Expr *E = Cursor->getAsExpr()) { SourceLoc SL = E->getStartLoc(); - return SM.getLineAndColumn(SL); + return SM.getPresumedLineAndColumnForLoc(SL); } return std::make_pair(0, 0); @@ -204,13 +204,13 @@ class FormatContext { SourceLoc ElseLoc = If->getElseLoc(); // If we're at 'else', take the indent of 'if' and continue. if (ElseLoc.isValid() && - LineAndColumn.first == SM.getLineAndColumn(ElseLoc).first) { + LineAndColumn.first == SM.getPresumedLineAndColumnForLoc(ElseLoc).first) { LineAndColumn = ParentLineAndColumn; continue; } // If we are at conditions, take the indent of 'if' and continue. for (auto Cond : If->getCond()) { - if (LineAndColumn.first == SM.getLineNumber(Cond.getEndLoc())) { + if (LineAndColumn.first == SM.getLineAndColumnInBuffer(Cond.getEndLoc()).first) { LineAndColumn = ParentLineAndColumn; continue; } @@ -234,7 +234,7 @@ class FormatContext { // Align with Func start instead of with param decls. if (auto *FD = dyn_cast_or_null(Cursor->getAsDecl())) { - if (LineAndColumn.first <= SM.getLineNumber(FD->getSignatureSourceRange().End)) { + if (LineAndColumn.first <= SM.getLineAndColumnInBuffer(FD->getSignatureSourceRange().End).first) { LineAndColumn = ParentLineAndColumn; continue; } @@ -252,7 +252,7 @@ class FormatContext { } bool exprEndAtLine(Expr *E, unsigned Line) { - return E->getEndLoc().isValid() && SM.getLineNumber(E->getEndLoc()) == Line; + return E->getEndLoc().isValid() && SM.getLineAndColumnInBuffer(E->getEndLoc()).first == Line; }; bool shouldAddIndentForLine(unsigned Line, TokenInfo TInfo, @@ -274,7 +274,7 @@ class FormatContext { if (!LabelItems.empty()) Loc = LabelItems.back().getPattern()->getLoc(); if (Loc.isValid()) - return Line > SM.getLineAndColumn(Loc).first; + return Line > SM.getPresumedLineAndColumnForLoc(Loc).first; return true; } if (isSwitchContext()) { @@ -291,7 +291,7 @@ class FormatContext { // switch ... // { <-- No indent here, open brace should be at same level as switch. auto *S = cast(Cursor->getAsStmt()); - if (SM.getLineAndColumn(S->getLBraceLoc()).first == Line) + if (SM.getPresumedLineAndColumnForLoc(S->getLBraceLoc()).first == Line) return false; if (IsInCommentLine()) { for (auto Case : S->getCases()) { @@ -299,7 +299,7 @@ class FormatContext { // { // // case comment <-- No indent here. // case 0: - if (SM.getLineAndColumn(Case->swift::Stmt::getStartLoc()).first == Line + 1) + if (SM.getPresumedLineAndColumnForLoc(Case->swift::Stmt::getStartLoc()).first == Line + 1) return FmtOptions.IndentSwitchCase; } } @@ -317,7 +317,7 @@ class FormatContext { // } if (auto FD = dyn_cast_or_null(Start.getAsDecl())) { if (FD->isGetter() && FD->getAccessorKeywordLoc().isInvalid()) { - if (SM.getLineNumber(FD->getBody()->getLBraceLoc()) == Line) + if (SM.getLineAndColumnInBuffer(FD->getBody()->getLBraceLoc()).first == Line) return false; } } @@ -357,10 +357,10 @@ class FormatContext { // class Foo // { <-- No indent here, open brace should be at same level as class. auto *NTD = dyn_cast_or_null(Cursor->getAsDecl()); - if (NTD && SM.getLineAndColumn(NTD->getBraces().Start).first == Line) + if (NTD && SM.getPresumedLineAndColumnForLoc(NTD->getBraces().Start).first == Line) return false; auto *ETD = dyn_cast_or_null(Cursor->getAsDecl()); - if (ETD && SM.getLineAndColumn(ETD->getBraces().Start).first == Line) + if (ETD && SM.getPresumedLineAndColumnForLoc(ETD->getBraces().Start).first == Line) return false; // If we are at the start of a trailing closure, do not add indentation. @@ -369,21 +369,21 @@ class FormatContext { // { <-- No indent here. auto *TE = dyn_cast_or_null(Cursor->getAsExpr()); if (TE && TE->hasTrailingClosure() && - SM.getLineNumber(TE->getElements().back()->getStartLoc()) == Line) { + SM.getLineAndColumnInBuffer(TE->getElements().back()->getStartLoc()).first == Line) { return false; } // If we're in an IfStmt and at the 'else', don't add an indent. IfStmt *If = dyn_cast_or_null(Cursor->getAsStmt()); if (If && If->getElseLoc().isValid() && - SM.getLineAndColumn(If->getElseLoc()).first == Line) + SM.getPresumedLineAndColumnForLoc(If->getElseLoc()).first == Line) return false; // If we're in a DoCatchStmt and at a 'catch', don't add an indent. if (auto *DoCatchS = dyn_cast_or_null(Cursor->getAsStmt())) { for (CatchStmt *CatchS : DoCatchS->getCatches()) { SourceLoc Loc = CatchS->getCatchLoc(); - if (Loc.isValid() && SM.getLineAndColumn(Loc).first == Line) + if (Loc.isValid() && SM.getPresumedLineAndColumnForLoc(Loc).first == Line) return false; } } @@ -403,24 +403,24 @@ class FormatContext { if (auto *Paren = dyn_cast_or_null(Cursor->getAsExpr())) { auto *SubExpr = Paren->getSubExpr(); if (SubExpr && SubExpr == AtExprEnd && - SM.getLineAndColumn(Paren->getEndLoc()).first == Line) + SM.getPresumedLineAndColumnForLoc(Paren->getEndLoc()).first == Line) return false; } else if (auto *Tuple = dyn_cast_or_null(Cursor->getAsExpr())) { auto SubExprs = Tuple->getElements(); if (!SubExprs.empty() && SubExprs.back() == AtExprEnd && - SM.getLineAndColumn(Tuple->getEndLoc()).first == Line) { + SM.getPresumedLineAndColumnForLoc(Tuple->getEndLoc()).first == Line) { return false; } } else if (auto *VD = dyn_cast_or_null(Cursor->getAsDecl())) { SourceLoc Loc = getVarDeclInitEnd(VD); - if (Loc.isValid() && SM.getLineNumber(Loc) == Line) { + if (Loc.isValid() && SM.getLineAndColumnInBuffer(Loc).first == Line) { return false; } } else if (auto *Seq = dyn_cast_or_null(Cursor->getAsExpr())) { ArrayRef Elements = Seq->getElements(); if (Elements.size() == 3 && isa(Elements[1]) && - SM.getLineAndColumn(Elements[2]->getEndLoc()).first == Line) { + SM.getPresumedLineAndColumnForLoc(Elements[2]->getEndLoc()).first == Line) { return false; } } @@ -524,7 +524,8 @@ class FormatWalker : public SourceEntityWalker { } bool sameLineWithTarget(SourceLoc Loc) { - return SM.getLineNumber(Loc) == SM.getLineNumber(TargetLoc); + return SM.getLineAndColumnInBuffer(Loc).first == + SM.getLineAndColumnInBuffer(TargetLoc).first; } public: @@ -539,7 +540,7 @@ class FormatWalker : public SourceEntityWalker { SourceLoc PrevLoc; auto FindAlignLoc = [&](SourceLoc Loc) { if (PrevLoc.isValid() && Loc.isValid() && - SM.getLineNumber(PrevLoc) == SM.getLineNumber(Loc)) + SM.getLineAndColumnInBuffer(PrevLoc).first == SM.getLineAndColumnInBuffer(Loc).first) return PrevLoc; return PrevLoc = Loc; }; @@ -693,8 +694,8 @@ class FormatWalker : public SourceEntityWalker { (InValid || SM.isBeforeInBuffer(CurrentTokIt->getLoc(), Loc)); CurrentTokIt++) { if (CurrentTokIt->getKind() == tok::comment) { - auto StartLine = SM.getLineNumber(CurrentTokIt->getRange().getStart()); - auto EndLine = SM.getLineNumber(CurrentTokIt->getRange().getEnd()); + auto StartLine = SM.getLineAndColumnInBuffer(CurrentTokIt->getRange().getStart()).first; + auto EndLine = SM.getLineAndColumnInBuffer(CurrentTokIt->getRange().getEnd()).first; auto TokenStr = CurrentTokIt->getRange().str(); InDocCommentBlock |= TargetLine > StartLine && TargetLine <= EndLine && TokenStr.startswith("/*"); @@ -721,7 +722,7 @@ class FormatWalker : public SourceEntityWalker { FormatContext walkToLocation(SourceLoc Loc) { Stack.clear(); TargetLocation = Loc; - TargetLine = SM.getLineNumber(TargetLocation); + TargetLine = SM.getLineAndColumnInBuffer(TargetLocation).first; AtStart = AtEnd = swift::ASTWalker::ParentTy(); walk(SF); scanForComments(SourceLoc()); @@ -857,10 +858,10 @@ class TokenInfoCollector { SourceManager &SM; Comparator(SourceManager &SM) : SM(SM) {} bool operator()(const Token &T, unsigned Line) const { - return SM.getLineNumber(T.getLoc()) < Line; + return SM.getLineAndColumnInBuffer(T.getLoc()).first < Line; } bool operator()(unsigned Line, const Token &T) const { - return Line < SM.getLineNumber(T.getLoc()); + return Line < SM.getLineAndColumnInBuffer(T.getLoc()).first; } }; @@ -873,7 +874,7 @@ class TokenInfoCollector { return TokenInfo(); Comparator Comp(SM); auto LineMatch = [this] (const Token* T, unsigned Line) { - return T != Tokens.end() && SM.getLineNumber(T->getLoc()) == Line; + return T != Tokens.end() && SM.getLineAndColumnInBuffer(T->getLoc()).first == Line; }; auto TargetIt = std::lower_bound(Tokens.begin(), Tokens.end(), Line, Comp); auto LineBefore = std::lower_bound(Tokens.begin(), TargetIt, Line - 1, Comp); diff --git a/lib/Index/Index.cpp b/lib/Index/Index.cpp index 5b2b3ca32ba35..f7706f91abd78 100644 --- a/lib/Index/Index.cpp +++ b/lib/Index/Index.cpp @@ -399,7 +399,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker { std::pair getLineCol(SourceLoc Loc) { if (Loc.isInvalid()) return std::make_pair(0, 0); - return SrcMgr.getLineAndColumn(Loc, BufferID); + return SrcMgr.getPresumedLineAndColumnForLoc(Loc, BufferID); } bool shouldIndex(ValueDecl *D, bool IsRef) const { diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index 8497d3cec257d..6f3535164bd5b 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -2129,15 +2129,6 @@ static const char *findStartOfLine(const char *bufStart, const char *current) { return current; } -SourceLoc Lexer::getLocForStartOfToken(SourceManager &SM, SourceLoc Loc) { - Optional BufferIdOp = SM.getIDForBufferIdentifier(SM. - getBufferIdentifierForLoc(Loc)); - if (!BufferIdOp.hasValue()) - return SourceLoc(); - return getLocForStartOfToken(SM, BufferIdOp.getValue(), - SM.getLocOffsetInBuffer(Loc, BufferIdOp.getValue())); -} - SourceLoc Lexer::getLocForStartOfToken(SourceManager &SM, unsigned BufferID, unsigned Offset) { CharSourceRange entireRange = SM.getRangeForBuffer(BufferID); diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index cea2db5a967ed..685082a598142 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2987,7 +2987,8 @@ ParserStatus Parser::parseLineDirective(bool isLine) { return makeParserError(); } - int LineOffset = StartLine - SourceMgr.getLineNumber(nextLineStartLoc); + int LineOffset = + StartLine - SourceMgr.getLineAndColumnInBuffer(nextLineStartLoc).first; // Create a new virtual file for the region started by the #line marker. bool isNewFile = SourceMgr.openVirtualFile(nextLineStartLoc, diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index 0d96a6d65d002..bc8a9c3d34825 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -2658,8 +2658,8 @@ ParserResult Parser::parseTrailingClosure(SourceRange calleeRange) { // Warn if the trailing closure is separated from its callee by more than // one line. A single-line separation is acceptable for a trailing closure // call, and will be diagnosed later only if the call fails to typecheck. - auto origLine = SourceMgr.getLineNumber(calleeRange.End); - auto braceLine = SourceMgr.getLineNumber(braceLoc); + auto origLine = SourceMgr.getLineAndColumnInBuffer(calleeRange.End).first; + auto braceLine = SourceMgr.getLineAndColumnInBuffer(braceLoc).first; if (braceLine > origLine + 1) { diagnose(braceLoc, diag::trailing_closure_after_newlines); diagnose(calleeRange.Start, diag::trailing_closure_callee_here); diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index f670b8b21828f..e94dec7d82c24 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -661,8 +661,8 @@ ParserResult Parser::parseStmtReturn(SourceLoc tryLoc) { // Issue a warning when the returned expression is on a different line than // the return keyword, but both have the same indentation. - if (SourceMgr.getLineAndColumn(ReturnLoc).second == - SourceMgr.getLineAndColumn(ExprLoc).second) { + if (SourceMgr.getLineAndColumnInBuffer(ReturnLoc).second == + SourceMgr.getLineAndColumnInBuffer(ExprLoc).second) { diagnose(ExprLoc, diag::unindented_code_after_return); diagnose(ExprLoc, diag::indent_expression_to_silence); } diff --git a/lib/SIL/SILLocation.cpp b/lib/SIL/SILLocation.cpp index 1024b278e2bb9..7eafa4ab63296 100644 --- a/lib/SIL/SILLocation.cpp +++ b/lib/SIL/SILLocation.cpp @@ -148,8 +148,8 @@ SILLocation::DebugLoc SILLocation::decode(SourceLoc Loc, const SourceManager &SM) { DebugLoc DL; if (Loc.isValid()) { - DL.Filename = SM.getBufferIdentifierForLoc(Loc); - std::tie(DL.Line, DL.Column) = SM.getLineAndColumn(Loc); + DL.Filename = SM.getPresumedFilenameForLoc(Loc); + std::tie(DL.Line, DL.Column) = SM.getPresumedLineAndColumnForLoc(Loc); } return DL; } diff --git a/lib/SILGen/SILGenApply.cpp b/lib/SILGen/SILGenApply.cpp index 3ef2e74201848..13767981d0641 100644 --- a/lib/SILGen/SILGenApply.cpp +++ b/lib/SILGen/SILGenApply.cpp @@ -4452,7 +4452,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) { case MagicIdentifierLiteralExpr::File: { StringRef value = ""; if (loc.isValid()) - value = ctx.SourceMgr.getBufferIdentifierForLoc(loc); + value = ctx.SourceMgr.getPresumedFilenameForLoc(loc); builtinLiteralArgs = emitStringLiteral(*this, literal, value, C, magicLiteral->getStringEncoding()); builtinInit = magicLiteral->getBuiltinInitializer(); diff --git a/lib/SILGen/SILGenConvert.cpp b/lib/SILGen/SILGenConvert.cpp index 01632e18c3a78..921188f456a45 100644 --- a/lib/SILGen/SILGenConvert.cpp +++ b/lib/SILGen/SILGenConvert.cpp @@ -142,9 +142,8 @@ static void emitSourceLocationArgs(SILGenFunction &gen, StringRef filename = ""; unsigned line = 0; if (sourceLoc.isValid()) { - unsigned bufferID = ctx.SourceMgr.findBufferContainingLoc(sourceLoc); - filename = ctx.SourceMgr.getIdentifierForBuffer(bufferID); - line = ctx.SourceMgr.getLineAndColumn(sourceLoc).first; + filename = ctx.SourceMgr.getPresumedFilenameForLoc(sourceLoc); + line = ctx.SourceMgr.getPresumedLineAndColumnForLoc(sourceLoc).first; } bool isASCII = true; diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp index 0c8f157a91c6a..4c76995e82c7e 100644 --- a/lib/SILGen/SILGenExpr.cpp +++ b/lib/SILGen/SILGenExpr.cpp @@ -2405,7 +2405,7 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) { case MagicIdentifierLiteralExpr::Line: { unsigned Value = 0; if (Loc.isValid()) - Value = Ctx.SourceMgr.getLineAndColumn(Loc).first; + Value = Ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).first; SILValue V = SGF.B.createIntegerLiteral(E, Ty, Value); return RValue(SGF, E, ManagedValue::forUnmanaged(V)); @@ -2413,7 +2413,7 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) { case MagicIdentifierLiteralExpr::Column: { unsigned Value = 0; if (Loc.isValid()) - Value = Ctx.SourceMgr.getLineAndColumn(Loc).second; + Value = Ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).second; SILValue V = SGF.B.createIntegerLiteral(E, Ty, Value); return RValue(SGF, E, ManagedValue::forUnmanaged(V)); diff --git a/lib/SILGen/SILGenProfiling.cpp b/lib/SILGen/SILGenProfiling.cpp index 6beb720560261..5ece8daab4b73 100644 --- a/lib/SILGen/SILGenProfiling.cpp +++ b/lib/SILGen/SILGenProfiling.cpp @@ -482,8 +482,8 @@ struct CoverageMapping : public ASTWalker { assert(Region.hasStartLoc() && "invalid region"); assert(Region.hasEndLoc() && "incomplete region"); - auto Start = SM.getLineAndColumn(Region.getStartLoc()); - auto End = SM.getLineAndColumn(Region.getEndLoc()); + auto Start = SM.getPresumedLineAndColumnForLoc(Region.getStartLoc()); + auto End = SM.getPresumedLineAndColumnForLoc(Region.getEndLoc()); assert(Start.first <= End.first && "region start and end out of order"); Regions.emplace_back(Start.first, Start.second, End.first, End.second, diff --git a/lib/Sema/CSDiag.cpp b/lib/Sema/CSDiag.cpp index 2f83b6a5e44b2..ab52467260ba1 100644 --- a/lib/Sema/CSDiag.cpp +++ b/lib/Sema/CSDiag.cpp @@ -5896,8 +5896,8 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) { auto &SM = CS->getASTContext().SourceMgr; if (closure->hasAnonymousClosureVars() && closure->getParameters()->size() == 0 && - 1 + SM.getLineNumber(callExpr->getFn()->getEndLoc()) == - SM.getLineNumber(closure->getStartLoc())) { + 1 + SM.getLineAndColumnInBuffer(callExpr->getFn()->getEndLoc()).first == + SM.getLineAndColumnInBuffer(closure->getStartLoc()).first) { diagnose(closure->getStartLoc(), diag::brace_stmt_suggest_do) .fixItInsert(closure->getStartLoc(), "do "); } diff --git a/lib/Sema/MiscDiagnostics.cpp b/lib/Sema/MiscDiagnostics.cpp index 7844283389a1b..3930ee897f35a 100644 --- a/lib/Sema/MiscDiagnostics.cpp +++ b/lib/Sema/MiscDiagnostics.cpp @@ -2683,8 +2683,8 @@ static void checkSwitch(TypeChecker &TC, const SwitchStmt *stmt) { continue; auto &SM = TC.Context.SourceMgr; - auto prevLineCol = SM.getLineAndColumn(prevLoc); - if (SM.getLineNumber(thisLoc) != prevLineCol.first) + auto prevLineCol = SM.getLineAndColumnInBuffer(prevLoc); + if (SM.getLineAndColumnInBuffer(thisLoc).first != prevLineCol.first) continue; TC.diagnose(items[i].getWhereLoc(), diag::where_on_one_item) diff --git a/lib/Sema/PCMacro.cpp b/lib/Sema/PCMacro.cpp index fb1a5ca3eee4c..7bbe9e9aeb8a1 100644 --- a/lib/Sema/PCMacro.cpp +++ b/lib/Sema/PCMacro.cpp @@ -527,10 +527,11 @@ class Instrumenter : InstrumenterBase { } std::pair StartLC = - Context.SourceMgr.getLineAndColumn(SR.Start); + Context.SourceMgr.getPresumedLineAndColumnForLoc(SR.Start); - std::pair EndLC = Context.SourceMgr.getLineAndColumn( - Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End)); + std::pair EndLC = + Context.SourceMgr.getPresumedLineAndColumnForLoc( + Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End)); const size_t buf_size = 8; @@ -610,10 +611,11 @@ class Instrumenter : InstrumenterBase { } std::pair StartLC = - Context.SourceMgr.getLineAndColumn(SR.Start); + Context.SourceMgr.getPresumedLineAndColumnForLoc(SR.Start); - std::pair EndLC = Context.SourceMgr.getLineAndColumn( - Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End)); + std::pair EndLC = + Context.SourceMgr.getPresumedLineAndColumnForLoc( + Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End)); const size_t buf_size = 8; diff --git a/lib/Sema/PlaygroundTransform.cpp b/lib/Sema/PlaygroundTransform.cpp index 73516c115e745..c9676410c8b91 100644 --- a/lib/Sema/PlaygroundTransform.cpp +++ b/lib/Sema/PlaygroundTransform.cpp @@ -806,9 +806,10 @@ class Instrumenter : InstrumenterBase { } std::pair StartLC = - Context.SourceMgr.getLineAndColumn(SR.Start); + Context.SourceMgr.getPresumedLineAndColumnForLoc(SR.Start); - std::pair EndLC = Context.SourceMgr.getLineAndColumn( + std::pair EndLC = + Context.SourceMgr.getPresumedLineAndColumnForLoc( Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End)); const size_t buf_size = 8; diff --git a/lib/Sema/TypeCheckPattern.cpp b/lib/Sema/TypeCheckPattern.cpp index c9c360288de85..8446f4fe7c3f3 100644 --- a/lib/Sema/TypeCheckPattern.cpp +++ b/lib/Sema/TypeCheckPattern.cpp @@ -689,8 +689,8 @@ static void diagnoseAndMigrateVarParameterToBody(ParamDecl *decl, std::string start; std::string end; - auto lBraceLine = SM.getLineNumber(declBody->getLBraceLoc()); - auto rBraceLine = SM.getLineNumber(declBody->getRBraceLoc()); + auto lBraceLine = SM.getLineAndColumnInBuffer(declBody->getLBraceLoc()).first; + auto rBraceLine = SM.getLineAndColumnInBuffer(declBody->getRBraceLoc()).first; if (!declBody->getNumElements()) { @@ -711,7 +711,7 @@ static void diagnoseAndMigrateVarParameterToBody(ParamDecl *decl, } else { auto firstLine = declBody->getElement(0); insertionStartLoc = firstLine.getStartLoc(); - if (lBraceLine == SM.getLineNumber(firstLine.getStartLoc())) { + if (lBraceLine == SM.getLineAndColumnInBuffer(firstLine.getStartLoc()).first) { // Function on same line, insert with semi-colon. Not ideal but // better than weird space alignment. start = ""; diff --git a/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp b/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp index ac9ca49a928bb..c1a7e45fca423 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp +++ b/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp @@ -105,7 +105,7 @@ void EditorDiagConsumer::handleDiagnostic(SourceManager &SM, SourceLoc Loc, } SKInfo.Offset = SM.getLocOffsetInBuffer(Loc, BufferID); - std::tie(SKInfo.Line, SKInfo.Column) = SM.getLineAndColumn(Loc, BufferID); + std::tie(SKInfo.Line, SKInfo.Column) = SM.getPresumedLineAndColumnForLoc(Loc, BufferID); SKInfo.Filename = SM.getIdentifierForBuffer(BufferID); for (auto R : Info.Ranges) { @@ -1250,8 +1250,8 @@ class SwiftEditorSyntaxWalker: public ide::SyntaxModelWalker { ++NestingLevel; SourceLoc StartLoc = Node.Range.getStart(); - auto StartLineAndColumn = SrcManager.getLineAndColumn(StartLoc); - auto EndLineAndColumn = SrcManager.getLineAndColumn(Node.Range.getEnd()); + auto StartLineAndColumn = SrcManager.getPresumedLineAndColumnForLoc(StartLoc); + auto EndLineAndColumn = SrcManager.getPresumedLineAndColumnForLoc(Node.Range.getEnd()); unsigned StartLine = StartLineAndColumn.first; unsigned EndLine = EndLineAndColumn.second > 1 ? EndLineAndColumn.first : EndLineAndColumn.first - 1; @@ -1676,8 +1676,8 @@ ImmutableTextSnapshotRef SwiftEditorDocument::replaceText( unsigned BufID = Impl.SyntaxInfo->getBufferID(); SourceLoc StartLoc = SrcManager.getLocForBufferStart(BufID).getAdvancedLoc( Offset); - unsigned StartLine = SrcManager.getLineAndColumn(StartLoc).first; - unsigned EndLine = SrcManager.getLineAndColumn( + unsigned StartLine = SrcManager.getPresumedLineAndColumnForLoc(StartLoc).first; + unsigned EndLine = SrcManager.getPresumedLineAndColumnForLoc( StartLoc.getAdvancedLoc(Length)).first; // Delete all syntax map data from start line through end line. diff --git a/tools/swift-ide-test/swift-ide-test.cpp b/tools/swift-ide-test/swift-ide-test.cpp index 4c6f34093ce46..167ed10ea42e4 100644 --- a/tools/swift-ide-test/swift-ide-test.cpp +++ b/tools/swift-ide-test/swift-ide-test.cpp @@ -1207,7 +1207,7 @@ class AnnotationPrinter : public SourceEntityWalker { void printLoc(SourceLoc Loc, raw_ostream &OS) { OS << '@'; if (Loc.isValid()) { - auto LineCol = SM.getLineAndColumn(Loc, BufferID); + auto LineCol = SM.getPresumedLineAndColumnForLoc(Loc, BufferID); OS << LineCol.first << ':' << LineCol.second; } } @@ -2004,7 +2004,7 @@ class ASTTypePrinter : public ASTWalker { SourceCode = SM.extractText({ SR.Start, SM.getByteDistance(SR.Start, EndCharLoc) }); unsigned Column; - std::tie(Line, Column) = SM.getLineAndColumn(SR.Start, BufferID); + std::tie(Line, Column) = SM.getPresumedLineAndColumnForLoc(SR.Start, BufferID); } OS.indent(IndentLevel * 2); @@ -2220,7 +2220,7 @@ class ASTCommentPrinter : public ASTWalker { if (auto *VD = dyn_cast(D)) { SourceLoc Loc = D->getLoc(); if (Loc.isValid()) { - auto LineAndColumn = SM.getLineAndColumn(Loc); + auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc); OS << getBufferIdentifier(VD->getLoc()) << ":" << LineAndColumn.first << ":" << LineAndColumn.second << ": "; } @@ -2237,7 +2237,7 @@ class ASTCommentPrinter : public ASTWalker { } else if (isa(D)) { SourceLoc Loc = D->getLoc(); if (Loc.isValid()) { - auto LineAndColumn = SM.getLineAndColumn(Loc); + auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc); OS << getBufferIdentifier(D->getLoc()) << ":" << LineAndColumn.first << ":" << LineAndColumn.second << ": "; } @@ -2425,8 +2425,9 @@ static int doPrintTypeInterface(const CompilerInvocation &InitInvok, assert(SF && "no source file?"); SemaLocResolver Resolver(*SF); SourceManager &SM = SF->getASTContext().SourceMgr; - auto Offset = SM.resolveFromLineCol(BufID, Pair.getValue().first, - Pair.getValue().second); + auto Offset = SM.getOffsetForLineAndColumnInBuffer(BufID, + Pair.getValue().first, + Pair.getValue().second); if (!Offset.hasValue()) { llvm::errs() << "Cannot resolve source location.\n"; return 1; @@ -2512,7 +2513,7 @@ class USRPrinter : public SourceEntityWalker { void printLoc(SourceLoc Loc) { if (Loc.isValid()) { - auto LineCol = SM.getLineAndColumn(Loc, BufferID); + auto LineCol = SM.getPresumedLineAndColumnForLoc(Loc, BufferID); OS << LineCol.first << ':' << LineCol.second; } } @@ -2655,10 +2656,10 @@ static int doPrintRangeInfo(const CompilerInvocation &InitInvok, assert(SF->getBufferID().hasValue() && "no buffer id?"); SourceManager &SM = SF->getASTContext().SourceMgr; unsigned bufferID = SF->getBufferID().getValue(); - SourceLoc StartLoc = SM.getLocForLineCol(bufferID, StartLineCol.first, - StartLineCol.second); - SourceLoc EndLoc = SM.getLocForLineCol(bufferID, EndLineCol.first, - EndLineCol.second); + SourceLoc StartLoc = SM.getLocForLineAndColumnInBuffer( + bufferID, StartLineCol.first, StartLineCol.second); + SourceLoc EndLoc = SM.getLocForLineAndColumnInBuffer( + bufferID, EndLineCol.first, EndLineCol.second); RangeResolver Resolver(*SF, StartLoc, EndLoc); ResolvedRangeInfo Result = Resolver.resolve(); Result.print(llvm::outs());