Skip to content

Commit c8cd0f3

Browse files
author
David Ungar
committed
Move location lookup to RAII object.
1 parent 240b573 commit c8cd0f3

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace swift {
2828
class DiagnosticEngine;
2929
class SourceManager;
3030
class ValueDecl;
31-
31+
class SourceFile;
32+
3233
enum class PatternKind : uint8_t;
3334
enum class SelfAccessKind : uint8_t;
3435
enum class ReferenceOwnership : uint8_t;
@@ -807,12 +808,11 @@ namespace swift {
807808
static const char *diagnosticStringFor(const DiagID id);
808809

809810
/// If there is no clear .dia file for a diagnostic, put it in the one
810-
/// corresponding to the input buffer ID given here.
811+
/// corresponding to the SourceLoc given here.
811812
/// In particular, in batch mode when a diagnostic is located in
812813
/// a non-primary file, use this affordance to place it in the .dia
813814
/// file for the primary that is currently being worked on.
814-
void setBufferIndirectlyCausingDiagnosticToInput(
815-
unsigned bufferIDE);
815+
void setBufferIndirectlyCausingDiagnosticToInput(SourceLoc);
816816
void resetBufferIndirectlyCausingDiagnostic();
817817
SourceLoc getDefaultDiagnosticLoc() const {
818818
return bufferIndirectlyCausingDiagnostic;
@@ -822,14 +822,8 @@ namespace swift {
822822
class BufferIndirectlyCausingDiagnosticRAII {
823823
private:
824824
DiagnosticEngine &Diags;
825-
826825
public:
827-
BufferIndirectlyCausingDiagnosticRAII(DiagnosticEngine &Diags,
828-
unsigned bufferID)
829-
: Diags(Diags) {
830-
Diags.setBufferIndirectlyCausingDiagnosticToInput(
831-
bufferID);
832-
}
826+
BufferIndirectlyCausingDiagnosticRAII(const SourceFile &SF);
833827
~BufferIndirectlyCausingDiagnosticRAII() {
834828
Diags.resetBufferIndirectlyCausingDiagnostic();
835829
}

lib/AST/DiagnosticEngine.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,8 @@ const char *DiagnosticEngine::diagnosticStringFor(const DiagID id) {
848848
}
849849

850850
void DiagnosticEngine::setBufferIndirectlyCausingDiagnosticToInput(
851-
StringRef input) {
852-
if (input.empty()) {
853-
resetBufferIndirectlyCausingDiagnostic();
854-
return;
855-
}
856-
auto id = SourceMgr.getIDForBufferIdentifier(input);
857-
if (!id) {
858-
resetBufferIndirectlyCausingDiagnostic();
859-
return;
860-
}
861-
bufferIndirectlyCausingDiagnostic = SourceMgr.getLocForBufferStart(*id);
851+
SourceLoc loc) {
852+
bufferIndirectlyCausingDiagnostic = loc;
862853
}
863854
void DiagnosticEngine::resetBufferIndirectlyCausingDiagnostic() {
864855
bufferIndirectlyCausingDiagnostic = SourceLoc();
@@ -874,3 +865,13 @@ DiagnosticSuppression::~DiagnosticSuppression() {
874865
for (auto consumer : consumers)
875866
diags.addConsumer(*consumer);
876867
}
868+
869+
BufferIndirectlyCausingDiagnosticRAII::BufferIndirectlyCausingDiagnosticRAII(
870+
const SourceFile &SF)
871+
: Diags(SF.getASTContext().Diags) {
872+
auto id = SF.getBufferID();
873+
if (!id)
874+
return;
875+
auto loc = SF.getASTContext().SourceMgr.getLocForBufferStart(*id);
876+
Diags.setBufferIndirectlyCausingDiagnosticToInput(loc);
877+
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ static bool performCompileStepsPostSILGen(
12211221

12221222
Optional<BufferIndirectlyCausingDiagnosticRAII> ricd;
12231223
if (auto *SF = MSF.dyn_cast<SourceFile *>())
1224-
ricd.emplace(Context.Diags, SF->getBufferID());
1224+
ricd.emplace(*SF);
12251225

12261226
if (Stats)
12271227
countStatsPostSILGen(*Stats, *SM);

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
460460
return;
461461

462462
auto &Ctx = SF.getASTContext();
463-
BufferIndirectlyCausingDiagnosticRAII cpr(SF.getASTContext().Diags,
464-
SF.getBufferID());
463+
BufferIndirectlyCausingDiagnosticRAII cpr(SF);
465464

466465
// Make sure we have a type checker.
467466
TypeChecker &TC = createTypeChecker(Ctx);

0 commit comments

Comments
 (0)