From 90c2975d66c24005b69a3f74f2d56ebff221d970 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Tue, 9 Jul 2024 14:36:56 -0700 Subject: [PATCH] [Basic] Don't rewrite source buffer copy multiple times The on-disc buffer file name was not recorded correctly. rdar://130478685 --- include/swift/Basic/SourceManager.h | 5 ++--- lib/AST/Module.cpp | 2 +- lib/Basic/SourceLoc.cpp | 9 +++++---- lib/SILGen/SILGenFunction.cpp | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index 350ea0744d9..1a722c71a16 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -71,7 +71,7 @@ public: /// The name of the source file on disk that was created to hold the /// contents of this file for external clients. - StringRef onDiskBufferCopyFileName = StringRef(); + mutable StringRef onDiskBufferCopyFileName = StringRef(); /// Contains the ancestors of this source buffer, starting with the root source /// buffer and ending at this source buffer. @@ -209,8 +209,7 @@ public: bool hasGeneratedSourceInfo(unsigned bufferID); /// Retrieve the generated source information for the given buffer. - std::optional - getGeneratedSourceInfo(unsigned bufferID) const; + const GeneratedSourceInfo *getGeneratedSourceInfo(unsigned bufferID) const; /// Retrieve the list of ancestors of the given source buffer, starting with /// the root buffer and proceding to the given buffer ID at the end. diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index 429f18e7f4a..bd2717f41e3 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -907,7 +907,7 @@ ModuleDecl::getOriginalLocation(SourceLoc loc) const { SourceLoc startLoc = loc; unsigned startBufferID = bufferID; - while (std::optional info = + while (const GeneratedSourceInfo *info = SM.getGeneratedSourceInfo(bufferID)) { switch (info->kind) { #define MACRO_ROLE(Name, Description) \ diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index 07e2b5bc743..49eedea33b9 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -288,7 +288,8 @@ StringRef SourceManager::getIdentifierForBuffer( // If this is generated source code, and we're supposed to force it to disk // so external clients can see it, do so now. if (ForceGeneratedSourceToDisk) { - if (auto generatedInfo = getGeneratedSourceInfo(bufferID)) { + if (const GeneratedSourceInfo *generatedInfo = + getGeneratedSourceInfo(bufferID)) { // We only care about macros, so skip everything else. if (generatedInfo->kind == GeneratedSourceInfo::ReplacedFunctionBody || generatedInfo->kind == GeneratedSourceInfo::PrettyPrinted || @@ -403,12 +404,12 @@ bool SourceManager::hasGeneratedSourceInfo(unsigned bufferID) { return GeneratedSourceInfos.count(bufferID); } -std::optional +const GeneratedSourceInfo * SourceManager::getGeneratedSourceInfo(unsigned bufferID) const { auto known = GeneratedSourceInfos.find(bufferID); if (known == GeneratedSourceInfos.end()) - return std::nullopt; - return known->second; + return nullptr; + return &known->second; } namespace { diff --git a/lib/SILGen/SILGenFunction.cpp b/lib/SILGen/SILGenFunction.cpp index 5b8099d130d..4d50e653c4b 100644 --- a/lib/SILGen/SILGenFunction.cpp +++ b/lib/SILGen/SILGenFunction.cpp @@ -285,7 +285,7 @@ static DeclContext *getInnermostFunctionContext(DeclContext *DC) { } /// Return location of the macro expansion and the macro name. -static MacroInfo getMacroInfo(GeneratedSourceInfo &Info, +static MacroInfo getMacroInfo(const GeneratedSourceInfo &Info, DeclContext *FunctionDC) { MacroInfo Result(Info.generatedSourceRange.getStart(), Info.originalSourceRange.getStart());