Skip to content

[Driver] Move some string definitions around (NFC) #4493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "swift/Driver/Types.h"
#include "swift/Driver/Util.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/TimeValue.h"

namespace llvm {
Expand Down
27 changes: 13 additions & 14 deletions lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "llvm/Support/Timer.h"
#include "llvm/Support/YAMLParser.h"

#include "CompilationRecord.h"

using namespace swift;
using namespace swift::sys;
using namespace swift::driver;
Expand Down Expand Up @@ -190,27 +192,24 @@ static void writeCompilationRecord(StringRef path, StringRef argsHash,
out << "[" << time.seconds() << ", " << time.nanoseconds() << "]";
};

out << "version: \"" << llvm::yaml::escape(version::getSwiftFullVersion())
using compilation_record::TopLevelKey;
out << compilation_record::getName(TopLevelKey::Version) << ": \""
<< llvm::yaml::escape(version::getSwiftFullVersion())
<< "\"\n";
out << "options: \"" << llvm::yaml::escape(argsHash) << "\"\n";
out << "build_time: ";
out << compilation_record::getName(TopLevelKey::Options) << ": \""
<< llvm::yaml::escape(argsHash) << "\"\n";
out << compilation_record::getName(TopLevelKey::BuildTime) << ": ";
writeTimeValue(out, buildTime);
out << "\n";
out << "inputs:\n";
out << compilation_record::getName(TopLevelKey::Inputs) << ":\n";

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

switch (entry.second.status) {
case CompileJobAction::InputInfo::UpToDate:
break;
case CompileJobAction::InputInfo::NewlyAdded:
case CompileJobAction::InputInfo::NeedsCascadingBuild:
out << "!dirty ";
break;
case CompileJobAction::InputInfo::NeedsNonCascadingBuild:
out << "!private ";
break;
using compilation_record::getIdentifierForInputInfoStatus;
auto Name = getIdentifierForInputInfoStatus(entry.second.status);
if (!Name.empty()) {
out << Name << " ";
}

writeTimeValue(out, entry.second.previousModTime);
Expand Down
85 changes: 85 additions & 0 deletions lib/Driver/CompilationRecord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===--- CompilationRecord.h ------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_DRIVER_COMPILATIONRECORD_H
#define SWIFT_DRIVER_COMPILATIONRECORD_H

#include "swift/Driver/Action.h"

namespace swift {
namespace driver {
namespace compilation_record {

/// Compilation record files (.swiftdeps files) are YAML files composed of these
/// top-level keys.
enum class TopLevelKey {
/// The key for the Swift compiler version used to produce the compilation
/// record.
Version,
/// The key for the list of arguments passed to the Swift compiler when
/// producing the compilation record.
Options,
/// The key for the time at which the build that produced the compilation
/// record started.
BuildTime,
/// The key for the list of inputs to the compilation that produced the
/// compilation record.
Inputs,
};

/// \returns A string representation of the given key.
inline static StringRef getName(TopLevelKey Key) {
switch (Key) {
case TopLevelKey::Version: return "version";
case TopLevelKey::Options: return "options";
case TopLevelKey::BuildTime: return "build_time";
case TopLevelKey::Inputs: return "inputs";
}
}

/// \returns The string identifier used to represent the given status in a
/// compilation record file (.swiftdeps file).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last nitpick: can you add a note here pointing out that this will not round-trip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was actually confused when you mentioned that earlier. By "not round-trip", you mean that getInfoStatusForIdentifier(getIdentifierForInputInfoStatus(Status)) != Status, is that right? Since the left-hand side would be an Optional<Status>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note to the function below, based on my interpretation of "round trip" 😇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no, I mostly meant that there was no dedicated identifier for NewlyAdded, and that in general not every input status will necessarily have a unique compilation record identifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, gotcha. My bad! Updating now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Let me know if that's what you had in mind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. Thanks, Brian!

///
/// \note Not every InputInfo::Status has a unique identifier. For example,
/// both NewlyAdded and NeedsCascadingBuild are represented as "!dirty".
/// Therefore, this will not cleanly round-trip between InputInfo::Status and
/// string identifiers.
inline static StringRef
getIdentifierForInputInfoStatus(CompileJobAction::InputInfo::Status Status) {
switch (Status) {
case CompileJobAction::InputInfo::UpToDate:
return "";
case CompileJobAction::InputInfo::NewlyAdded:
case CompileJobAction::InputInfo::NeedsCascadingBuild:
return "!dirty";
case CompileJobAction::InputInfo::NeedsNonCascadingBuild:
return "!private";
}
}

/// \returns The status corresponding to the string identifier used in a
/// compilation record file (.swiftdeps file).
inline static Optional<CompileJobAction::InputInfo::Status>
getInfoStatusForIdentifier(StringRef Identifier) {
return llvm::StringSwitch<Optional<
CompileJobAction::InputInfo::Status>>(Identifier)
.Case("", CompileJobAction::InputInfo::UpToDate)
.Case("!dirty", CompileJobAction::InputInfo::NeedsCascadingBuild)
.Case("!private", CompileJobAction::InputInfo::NeedsNonCascadingBuild)
.Default(None);
}

} // end namespace compilation_record
} // end namespace driver
} // end namespace swift

#endif
19 changes: 9 additions & 10 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/raw_ostream.h"

#include "CompilationRecord.h"

#include <memory>

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

if (keyStr == "version") {
using compilation_record::TopLevelKey;
if (keyStr == compilation_record::getName(TopLevelKey::Version)) {
auto *value = dyn_cast<yaml::ScalarNode>(i->getValue());
if (!value)
return true;
versionValid =
(value->getValue(scratch) == version::getSwiftFullVersion());

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

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

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

using compilation_record::getInfoStatusForIdentifier;
auto previousBuildState =
llvm::StringSwitch<Optional<InputInfo::Status>>(value->getRawTag())
.Case("", InputInfo::UpToDate)
.Case("!dirty", InputInfo::NeedsCascadingBuild)
.Case("!private", InputInfo::NeedsNonCascadingBuild)
.Default(None);

getInfoStatusForIdentifier(value->getRawTag());
if (!previousBuildState)
return true;

Expand Down