mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[incrParse] Allow information about node reused be outputted
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
#define SWIFT_PARSE_SYNTAXPARSINGCACHE_H
|
||||
|
||||
#include "swift/Syntax/SyntaxNodes.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -53,6 +55,10 @@ class SyntaxParsingCache {
|
||||
/// the source file that is now parsed incrementally
|
||||
llvm::SmallVector<SourceEdit, 4> Edits;
|
||||
|
||||
/// If not \c nullptr information about syntax node reuse will be printed to
|
||||
/// this stream.
|
||||
llvm::raw_ostream *ReuseLog = nullptr;
|
||||
|
||||
public:
|
||||
SyntaxParsingCache(SourceFileSyntax OldSyntaxTree)
|
||||
: OldSyntaxTree(OldSyntaxTree) {}
|
||||
@@ -69,6 +75,18 @@ public:
|
||||
/// reused for a new syntax tree.
|
||||
llvm::Optional<Syntax> lookUp(size_t NewPosition, SyntaxKind Kind) const;
|
||||
|
||||
/// Specify a file to which information about node reuse shall be printed.
|
||||
void setReuseLog(StringRef Filename) {
|
||||
std::error_code ErrorCode;
|
||||
ReuseLog = new llvm::raw_fd_ostream(Filename, ErrorCode,
|
||||
llvm::sys::fs::OpenFlags::F_RW);
|
||||
assert(!ErrorCode && "Unable to open incremental usage log");
|
||||
}
|
||||
|
||||
~SyntaxParsingCache() {
|
||||
delete ReuseLog;
|
||||
}
|
||||
|
||||
private:
|
||||
llvm::Optional<Syntax> lookUpFrom(Syntax Node, size_t Position,
|
||||
SyntaxKind Kind) const;
|
||||
|
||||
@@ -35,6 +35,9 @@ llvm::Optional<Syntax> SyntaxParsingCache::lookUpFrom(Syntax Node,
|
||||
// token's leading trivia
|
||||
|
||||
if (!NodeEdited) {
|
||||
if (ReuseLog) {
|
||||
(*ReuseLog) << "Reused " << Kind << " at offset " << Position << '\n';
|
||||
}
|
||||
return Node;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,12 @@ IncrementalEdits("incremental-edit",
|
||||
"replace the selected range. "
|
||||
"Can be passed multiple times."));
|
||||
|
||||
static llvm::cl::opt<std::string>
|
||||
IncrementalReuseLog("incremental-reuse-log",
|
||||
llvm::cl::desc("Path to which a log should be written that "
|
||||
"describes all the nodes reused during "
|
||||
"incremental parsing."));
|
||||
|
||||
static llvm::cl::opt<std::string>
|
||||
OutputFilename("output-filename",
|
||||
llvm::cl::desc("Path to the output file"));
|
||||
@@ -331,8 +337,9 @@ int doIncrementalParse(const char *MainExecutablePath,
|
||||
llvm::errs() << "Could not deserialise old syntax tree.";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
SyntaxParsingCache *Cache = new SyntaxParsingCache(OldSyntaxTree.getValue());
|
||||
SyntaxParsingCache Cache = SyntaxParsingCache(OldSyntaxTree.getValue());
|
||||
|
||||
// Parse the source edits
|
||||
for (auto EditPattern : options::IncrementalEdits) {
|
||||
llvm::Regex MatchRegex("([0-9]+):([0-9]+)=(.*)");
|
||||
SmallVector<StringRef, 4> Matches;
|
||||
@@ -349,13 +356,18 @@ int doIncrementalParse(const char *MainExecutablePath,
|
||||
llvm::errs() << "Could not parse edit end as integer: " << EditStart;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
Cache->addEdit(EditStart, EditEnd, /*ReplacmentLength=*/Matches[3].size());
|
||||
Cache.addEdit(EditStart, EditEnd, /*ReplacmentLength=*/Matches[3].size());
|
||||
}
|
||||
|
||||
if (!options::IncrementalReuseLog.empty()) {
|
||||
StringRef Filename(options::IncrementalReuseLog.getValue());
|
||||
Cache.setReuseLog(Filename);
|
||||
}
|
||||
|
||||
// Parse the new libSyntax tree incrementally
|
||||
CompilerInstance Instance;
|
||||
SourceFile *SF =
|
||||
getSourceFile(Instance, InputFileName, MainExecutablePath, Cache);
|
||||
getSourceFile(Instance, InputFileName, MainExecutablePath, &Cache);
|
||||
|
||||
// Serialize and print the newly generated libSyntax tree
|
||||
auto Root = SF->getSyntaxRoot().getRaw();
|
||||
|
||||
Reference in New Issue
Block a user