2016-05-20 19:24:24 +08:00
|
|
|
//===--- TokenAnalyzer.cpp - Analyze Token Streams --------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-20 19:24:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements an abstract TokenAnalyzer and associated helper
|
2016-05-20 19:24:24 +08:00
|
|
|
/// classes. TokenAnalyzer can be extended to generate replacements based on
|
|
|
|
/// an annotated and pre-processed token stream.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TokenAnalyzer.h"
|
|
|
|
#include "AffectedRangeManager.h"
|
|
|
|
#include "Encoding.h"
|
|
|
|
#include "FormatToken.h"
|
|
|
|
#include "FormatTokenLexer.h"
|
|
|
|
#include "TokenAnnotator.h"
|
|
|
|
#include "UnwrappedLineParser.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "format-formatter"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2018-05-10 05:35:52 +08:00
|
|
|
Environment::Environment(StringRef Code, StringRef FileName,
|
|
|
|
ArrayRef<tooling::Range> Ranges,
|
|
|
|
unsigned FirstStartColumn, unsigned NextStartColumn,
|
|
|
|
unsigned LastStartColumn)
|
|
|
|
: VirtualSM(new SourceManagerForFile(FileName, Code)), SM(VirtualSM->get()),
|
|
|
|
ID(VirtualSM->get().getMainFileID()), FirstStartColumn(FirstStartColumn),
|
|
|
|
NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {
|
|
|
|
SourceLocation StartOfFile = SM.getLocForStartOfFile(ID);
|
2016-05-20 19:24:24 +08:00
|
|
|
for (const tooling::Range &Range : Ranges) {
|
|
|
|
SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
|
|
|
|
SourceLocation End = Start.getLocWithOffset(Range.getLength());
|
|
|
|
CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
|
|
|
|
: Style(Style), Env(Env),
|
|
|
|
AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
|
|
|
|
UnwrappedLines(1),
|
|
|
|
Encoding(encoding::detectEncoding(
|
|
|
|
Env.getSourceManager().getBufferData(Env.getFileID()))) {
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(
|
2016-05-20 19:24:24 +08:00
|
|
|
llvm::dbgs() << "File encoding: "
|
|
|
|
<< (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
|
|
|
|
<< "\n");
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
|
|
|
|
<< "\n");
|
2016-05-20 19:24:24 +08:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process() {
|
2016-05-20 19:24:24 +08:00
|
|
|
tooling::Replacements Result;
|
2020-07-06 20:02:54 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
|
|
|
|
IdentifierTable IdentTable(getFormattingLangOpts(Style));
|
|
|
|
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
|
|
|
|
Env.getFirstStartColumn(), Style, Encoding, Allocator,
|
2016-05-20 19:24:24 +08:00
|
|
|
|
2020-07-06 20:02:54 +08:00
|
|
|
IdentTable);
|
|
|
|
ArrayRef<FormatToken *> Toks(Lex.lex());
|
|
|
|
SmallVector<FormatToken *, 10> Tokens(Toks.begin(), Toks.end());
|
|
|
|
UnwrappedLineParser Parser(Style, Lex.getKeywords(),
|
|
|
|
Env.getFirstStartColumn(), Tokens, *this);
|
2016-05-20 19:24:24 +08:00
|
|
|
Parser.parse();
|
|
|
|
assert(UnwrappedLines.rbegin()->empty());
|
2017-10-30 22:01:50 +08:00
|
|
|
unsigned Penalty = 0;
|
2016-05-20 19:24:24 +08:00
|
|
|
for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; ++Run) {
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
|
2016-05-20 19:24:24 +08:00
|
|
|
SmallVector<AnnotatedLine *, 16> AnnotatedLines;
|
|
|
|
|
2020-07-06 20:02:54 +08:00
|
|
|
TokenAnnotator Annotator(Style, Lex.getKeywords());
|
2016-05-20 19:24:24 +08:00
|
|
|
for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
|
|
|
|
AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
|
|
|
|
Annotator.annotate(*AnnotatedLines.back());
|
|
|
|
}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
std::pair<tooling::Replacements, unsigned> RunResult =
|
2020-07-06 20:02:54 +08:00
|
|
|
analyze(Annotator, AnnotatedLines, Lex);
|
2016-05-20 19:24:24 +08:00
|
|
|
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG({
|
2016-05-20 19:24:24 +08:00
|
|
|
llvm::dbgs() << "Replacements for run " << Run << ":\n";
|
2017-10-30 22:01:50 +08:00
|
|
|
for (tooling::Replacements::const_iterator I = RunResult.first.begin(),
|
|
|
|
E = RunResult.first.end();
|
2016-05-20 19:24:24 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
llvm::dbgs() << I->toString() << "\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
|
|
|
delete AnnotatedLines[i];
|
|
|
|
}
|
2017-10-30 22:01:50 +08:00
|
|
|
|
|
|
|
Penalty += RunResult.second;
|
|
|
|
for (const auto &R : RunResult.first) {
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err = Result.add(R);
|
|
|
|
// FIXME: better error handling here. For now, simply return an empty
|
|
|
|
// Replacements to indicate failure.
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2017-10-30 22:01:50 +08:00
|
|
|
return {tooling::Replacements(), 0};
|
2016-08-01 18:16:37 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 19:24:24 +08:00
|
|
|
}
|
2017-10-30 22:01:50 +08:00
|
|
|
return {Result, Penalty};
|
2016-05-20 19:24:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TokenAnalyzer::consumeUnwrappedLine(const UnwrappedLine &TheLine) {
|
|
|
|
assert(!UnwrappedLines.empty());
|
|
|
|
UnwrappedLines.back().push_back(TheLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TokenAnalyzer::finishRun() {
|
|
|
|
UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|