Skip to content

Commit 19e6bee

Browse files
committedNov 12, 2020
[NFC] Use the Correct const-Qualification in Dependency Code
T *const does not prevent logically non-const accesses to the underlying data, it merely indicates that the pointer value itself is const. This modifier can be cast off by a copy, so it's not generally what you want here. Switch to const T * instead.
1 parent 13c97a8 commit 19e6bee

5 files changed

+29
-28
lines changed
 

‎include/swift/AST/FineGrainedDependencies.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class BiIndexedTwoStageMap {
351351
///
352352
/// \Note The returned graph should not be escaped from the callback.
353353
bool withReferenceDependencies(
354-
llvm::PointerUnion<ModuleDecl *, SourceFile *> MSF,
354+
llvm::PointerUnion<const ModuleDecl *, const SourceFile *> MSF,
355355
const DependencyTracker &depTracker, StringRef outputPath,
356356
bool alsoEmitDotFile, llvm::function_ref<bool(SourceFileDepGraph &&)>);
357357

‎lib/AST/FrontendSourceFileDepGraphFactory.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ std::string DependencyKey::computeNameForProvidedEntity<
193193
//==============================================================================
194194

195195
bool fine_grained_dependencies::withReferenceDependencies(
196-
llvm::PointerUnion<ModuleDecl *, SourceFile *> MSF,
196+
llvm::PointerUnion<const ModuleDecl *, const SourceFile *> MSF,
197197
const DependencyTracker &depTracker, StringRef outputPath,
198198
bool alsoEmitDotFile,
199199
llvm::function_ref<bool(SourceFileDepGraph &&)> cont) {
200-
if (auto *MD = MSF.dyn_cast<ModuleDecl *>()) {
200+
if (auto *MD = MSF.dyn_cast<const ModuleDecl *>()) {
201201
SourceFileDepGraph g =
202202
ModuleDepGraphFactory(MD, alsoEmitDotFile).construct();
203203
return cont(std::move(g));
204204
} else {
205-
auto *SF = MSF.get<SourceFile *>();
205+
auto *SF = MSF.get<const SourceFile *>();
206206
SourceFileDepGraph g = FrontendSourceFileDepGraphFactory(
207207
SF, outputPath, depTracker, alsoEmitDotFile)
208208
.construct();
@@ -215,22 +215,22 @@ bool fine_grained_dependencies::withReferenceDependencies(
215215
//==============================================================================
216216

217217
FrontendSourceFileDepGraphFactory::FrontendSourceFileDepGraphFactory(
218-
SourceFile *SF, StringRef outputPath, const DependencyTracker &depTracker,
219-
const bool alsoEmitDotFile)
218+
const SourceFile *SF, StringRef outputPath,
219+
const DependencyTracker &depTracker, const bool alsoEmitDotFile)
220220
: AbstractSourceFileDepGraphFactory(
221-
SF->getASTContext().hadError(),
222-
outputPath, getInterfaceHash(SF), alsoEmitDotFile,
223-
SF->getASTContext().Diags),
221+
SF->getASTContext().hadError(), outputPath, getInterfaceHash(SF),
222+
alsoEmitDotFile, SF->getASTContext().Diags),
224223
SF(SF), depTracker(depTracker) {}
225224

226225
/// Centralize the invariant that the fingerprint of the whole file is the
227226
/// interface hash
228-
std::string FrontendSourceFileDepGraphFactory::getFingerprint(SourceFile *SF) {
227+
std::string
228+
FrontendSourceFileDepGraphFactory::getFingerprint(const SourceFile *SF) {
229229
return getInterfaceHash(SF);
230230
}
231231

232232
std::string
233-
FrontendSourceFileDepGraphFactory::getInterfaceHash(SourceFile *SF) {
233+
FrontendSourceFileDepGraphFactory::getInterfaceHash(const SourceFile *SF) {
234234
llvm::SmallString<32> interfaceHash;
235235
SF->getInterfaceHash(interfaceHash);
236236
return interfaceHash.str().str();
@@ -415,7 +415,7 @@ void FrontendSourceFileDepGraphFactory::addAllDefinedDecls() {
415415
namespace {
416416
/// Extracts uses out of a SourceFile
417417
class UsedDeclEnumerator {
418-
SourceFile *SF;
418+
const SourceFile *SF;
419419
const DependencyTracker &depTracker;
420420
StringRef swiftDeps;
421421

@@ -427,16 +427,16 @@ class UsedDeclEnumerator {
427427

428428
public:
429429
UsedDeclEnumerator(
430-
SourceFile *SF, const DependencyTracker &depTracker, StringRef swiftDeps,
430+
const SourceFile *SF, const DependencyTracker &depTracker,
431+
StringRef swiftDeps,
431432
function_ref<void(const DependencyKey &, const DependencyKey &)>
432433
createDefUse)
433434
: SF(SF), depTracker(depTracker), swiftDeps(swiftDeps),
434435
sourceFileInterface(DependencyKey::createKeyForWholeSourceFile(
435436
DeclAspect::interface, swiftDeps)),
436437
sourceFileImplementation(DependencyKey::createKeyForWholeSourceFile(
437438
DeclAspect::implementation, swiftDeps)),
438-
createDefUse(createDefUse) {
439-
}
439+
createDefUse(createDefUse) {}
440440

441441
public:
442442
void enumerateAllUses() {
@@ -517,10 +517,11 @@ void FrontendSourceFileDepGraphFactory::addAllUsedDecls() {
517517
// MARK: ModuleDepGraphFactory
518518
//==============================================================================
519519

520-
ModuleDepGraphFactory::ModuleDepGraphFactory(ModuleDecl *Mod, bool emitDot)
521-
: AbstractSourceFileDepGraphFactory(
522-
Mod->getASTContext().hadError(),
523-
Mod->getNameStr(), "0xBADBEEF", emitDot, Mod->getASTContext().Diags),
520+
ModuleDepGraphFactory::ModuleDepGraphFactory(const ModuleDecl *Mod,
521+
bool emitDot)
522+
: AbstractSourceFileDepGraphFactory(Mod->getASTContext().hadError(),
523+
Mod->getNameStr(), "0xBADBEEF", emitDot,
524+
Mod->getASTContext().Diags),
524525
Mod(Mod) {}
525526

526527
void ModuleDepGraphFactory::addAllDefinedDecls() {

‎lib/AST/FrontendSourceFileDepGraphFactory.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@ namespace fine_grained_dependencies {
2323

2424
class FrontendSourceFileDepGraphFactory
2525
: public AbstractSourceFileDepGraphFactory {
26-
SourceFile *const SF;
26+
const SourceFile *SF;
2727
const DependencyTracker &depTracker;
2828

2929
public:
30-
FrontendSourceFileDepGraphFactory(SourceFile *SF, StringRef outputPath,
30+
FrontendSourceFileDepGraphFactory(const SourceFile *SF, StringRef outputPath,
3131
const DependencyTracker &depTracker,
3232
bool alsoEmitDotFile);
3333

3434
~FrontendSourceFileDepGraphFactory() override = default;
3535

3636
private:
37-
static std::string getFingerprint(SourceFile *SF);
38-
static std::string getInterfaceHash(SourceFile *SF);
37+
static std::string getFingerprint(const SourceFile *SF);
38+
static std::string getInterfaceHash(const SourceFile *SF);
3939

4040
void addAllDefinedDecls() override;
4141
void addAllUsedDecls() override;
4242
};
4343

4444
class ModuleDepGraphFactory : public AbstractSourceFileDepGraphFactory {
45-
ModuleDecl *const Mod;
45+
const ModuleDecl *Mod;
4646

4747
public:
48-
ModuleDepGraphFactory(ModuleDecl *Mod, bool emitDot);
48+
ModuleDepGraphFactory(const ModuleDecl *Mod, bool emitDot);
4949

5050
~ModuleDepGraphFactory() override = default;
5151

‎lib/FrontendTool/FrontendTool.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ emitLoadedModuleTraceForAllPrimariesIfNeeded(ModuleDecl *mainModule,
116116
const FrontendOptions &opts) {
117117
opts.InputsAndOutputs.forEachInputProducingSupplementaryOutput(
118118
[&](const InputFile &input) -> bool {
119-
return emitLoadedModuleTraceIfNeeded(mainModule, depTracker, opts,
120-
input);
119+
return swift::emitLoadedModuleTraceIfNeeded(mainModule, depTracker,
120+
opts, input);
121121
});
122122
}
123123

‎lib/FrontendTool/LoadedModuleTrace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
699699
assert(!ctxt.hadError() &&
700700
"We should've already exited earlier if there was an error.");
701701

702-
StringRef loadedModuleTracePath = input.loadedModuleTracePath();
702+
auto loadedModuleTracePath = input.loadedModuleTracePath();
703703
if (loadedModuleTracePath.empty())
704704
return false;
705705
std::error_code EC;

0 commit comments

Comments
 (0)
Please sign in to comment.