Skip to content

Commit d08fe89

Browse files
authored
Merge pull request #4493 from modocache/driver-compilation-record
[Driver] Move some string definitions around (NFC)
2 parents f25be36 + 2b3647e commit d08fe89

File tree

4 files changed

+109
-24
lines changed

4 files changed

+109
-24
lines changed

include/swift/Driver/Action.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "swift/Driver/Types.h"
1818
#include "swift/Driver/Util.h"
1919
#include "llvm/ADT/ArrayRef.h"
20+
#include "llvm/ADT/Optional.h"
21+
#include "llvm/ADT/StringSwitch.h"
2022
#include "llvm/Support/TimeValue.h"
2123

2224
namespace llvm {

lib/Driver/Compilation.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "llvm/Support/Timer.h"
3737
#include "llvm/Support/YAMLParser.h"
3838

39+
#include "CompilationRecord.h"
40+
3941
using namespace swift;
4042
using namespace swift::sys;
4143
using namespace swift::driver;
@@ -190,27 +192,24 @@ static void writeCompilationRecord(StringRef path, StringRef argsHash,
190192
out << "[" << time.seconds() << ", " << time.nanoseconds() << "]";
191193
};
192194

193-
out << "version: \"" << llvm::yaml::escape(version::getSwiftFullVersion())
195+
using compilation_record::TopLevelKey;
196+
out << compilation_record::getName(TopLevelKey::Version) << ": \""
197+
<< llvm::yaml::escape(version::getSwiftFullVersion())
194198
<< "\"\n";
195-
out << "options: \"" << llvm::yaml::escape(argsHash) << "\"\n";
196-
out << "build_time: ";
199+
out << compilation_record::getName(TopLevelKey::Options) << ": \""
200+
<< llvm::yaml::escape(argsHash) << "\"\n";
201+
out << compilation_record::getName(TopLevelKey::BuildTime) << ": ";
197202
writeTimeValue(out, buildTime);
198203
out << "\n";
199-
out << "inputs:\n";
204+
out << compilation_record::getName(TopLevelKey::Inputs) << ":\n";
200205

201206
for (auto &entry : inputs) {
202207
out << " \"" << llvm::yaml::escape(entry.first->getValue()) << "\": ";
203208

204-
switch (entry.second.status) {
205-
case CompileJobAction::InputInfo::UpToDate:
206-
break;
207-
case CompileJobAction::InputInfo::NewlyAdded:
208-
case CompileJobAction::InputInfo::NeedsCascadingBuild:
209-
out << "!dirty ";
210-
break;
211-
case CompileJobAction::InputInfo::NeedsNonCascadingBuild:
212-
out << "!private ";
213-
break;
209+
using compilation_record::getIdentifierForInputInfoStatus;
210+
auto Name = getIdentifierForInputInfoStatus(entry.second.status);
211+
if (!Name.empty()) {
212+
out << Name << " ";
214213
}
215214

216215
writeTimeValue(out, entry.second.previousModTime);

lib/Driver/CompilationRecord.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===--- CompilationRecord.h ------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_DRIVER_COMPILATIONRECORD_H
14+
#define SWIFT_DRIVER_COMPILATIONRECORD_H
15+
16+
#include "swift/Driver/Action.h"
17+
18+
namespace swift {
19+
namespace driver {
20+
namespace compilation_record {
21+
22+
/// Compilation record files (.swiftdeps files) are YAML files composed of these
23+
/// top-level keys.
24+
enum class TopLevelKey {
25+
/// The key for the Swift compiler version used to produce the compilation
26+
/// record.
27+
Version,
28+
/// The key for the list of arguments passed to the Swift compiler when
29+
/// producing the compilation record.
30+
Options,
31+
/// The key for the time at which the build that produced the compilation
32+
/// record started.
33+
BuildTime,
34+
/// The key for the list of inputs to the compilation that produced the
35+
/// compilation record.
36+
Inputs,
37+
};
38+
39+
/// \returns A string representation of the given key.
40+
inline static StringRef getName(TopLevelKey Key) {
41+
switch (Key) {
42+
case TopLevelKey::Version: return "version";
43+
case TopLevelKey::Options: return "options";
44+
case TopLevelKey::BuildTime: return "build_time";
45+
case TopLevelKey::Inputs: return "inputs";
46+
}
47+
}
48+
49+
/// \returns The string identifier used to represent the given status in a
50+
/// compilation record file (.swiftdeps file).
51+
///
52+
/// \note Not every InputInfo::Status has a unique identifier. For example,
53+
/// both NewlyAdded and NeedsCascadingBuild are represented as "!dirty".
54+
/// Therefore, this will not cleanly round-trip between InputInfo::Status and
55+
/// string identifiers.
56+
inline static StringRef
57+
getIdentifierForInputInfoStatus(CompileJobAction::InputInfo::Status Status) {
58+
switch (Status) {
59+
case CompileJobAction::InputInfo::UpToDate:
60+
return "";
61+
case CompileJobAction::InputInfo::NewlyAdded:
62+
case CompileJobAction::InputInfo::NeedsCascadingBuild:
63+
return "!dirty";
64+
case CompileJobAction::InputInfo::NeedsNonCascadingBuild:
65+
return "!private";
66+
}
67+
}
68+
69+
/// \returns The status corresponding to the string identifier used in a
70+
/// compilation record file (.swiftdeps file).
71+
inline static Optional<CompileJobAction::InputInfo::Status>
72+
getInfoStatusForIdentifier(StringRef Identifier) {
73+
return llvm::StringSwitch<Optional<
74+
CompileJobAction::InputInfo::Status>>(Identifier)
75+
.Case("", CompileJobAction::InputInfo::UpToDate)
76+
.Case("!dirty", CompileJobAction::InputInfo::NeedsCascadingBuild)
77+
.Case("!private", CompileJobAction::InputInfo::NeedsNonCascadingBuild)
78+
.Default(None);
79+
}
80+
81+
} // end namespace compilation_record
82+
} // end namespace driver
83+
} // end namespace swift
84+
85+
#endif

lib/Driver/Driver.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#include "llvm/Support/PrettyStackTrace.h"
5353
#include "llvm/Support/raw_ostream.h"
5454

55+
#include "CompilationRecord.h"
56+
5557
#include <memory>
5658

5759
using namespace swift;
@@ -262,20 +264,21 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
262264
auto *key = cast<yaml::ScalarNode>(i->getKey());
263265
StringRef keyStr = key->getValue(scratch);
264266

265-
if (keyStr == "version") {
267+
using compilation_record::TopLevelKey;
268+
if (keyStr == compilation_record::getName(TopLevelKey::Version)) {
266269
auto *value = dyn_cast<yaml::ScalarNode>(i->getValue());
267270
if (!value)
268271
return true;
269272
versionValid =
270273
(value->getValue(scratch) == version::getSwiftFullVersion());
271274

272-
} else if (keyStr == "options") {
275+
} else if (keyStr == compilation_record::getName(TopLevelKey::Options)) {
273276
auto *value = dyn_cast<yaml::ScalarNode>(i->getValue());
274277
if (!value)
275278
return true;
276279
optionsMatch = (argsHashStr == value->getValue(scratch));
277280

278-
} else if (keyStr == "build_time") {
281+
} else if (keyStr == compilation_record::getName(TopLevelKey::BuildTime)) {
279282
auto *value = dyn_cast<yaml::SequenceNode>(i->getValue());
280283
if (!value)
281284
return true;
@@ -284,7 +287,7 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
284287
return true;
285288
map[nullptr] = { InputInfo::NeedsCascadingBuild, timeVal };
286289

287-
} else if (keyStr == "inputs") {
290+
} else if (keyStr == compilation_record::getName(TopLevelKey::Inputs)) {
288291
auto *inputMap = dyn_cast<yaml::MappingNode>(i->getValue());
289292
if (!inputMap)
290293
return true;
@@ -300,13 +303,9 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
300303
if (!value)
301304
return true;
302305

306+
using compilation_record::getInfoStatusForIdentifier;
303307
auto previousBuildState =
304-
llvm::StringSwitch<Optional<InputInfo::Status>>(value->getRawTag())
305-
.Case("", InputInfo::UpToDate)
306-
.Case("!dirty", InputInfo::NeedsCascadingBuild)
307-
.Case("!private", InputInfo::NeedsNonCascadingBuild)
308-
.Default(None);
309-
308+
getInfoStatusForIdentifier(value->getRawTag());
310309
if (!previousBuildState)
311310
return true;
312311

0 commit comments

Comments
 (0)