24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/Debug.h"
30#define DEBUG_TYPE "format-formatter"
35class FormatTokenLexer;
119 if (LHS.
URL.empty() != RHS.
URL.empty())
120 return LHS.
URL.empty() < RHS.
URL.empty();
121 if (
int Res = LHS.
URL.compare_insensitive(RHS.
URL))
138 FileContents(
Env.getSourceManager().getBufferData(
Env.getFileID())) {
143 std::pair<tooling::Replacements, unsigned>
153 std::tie(References, FirstNonImportLine) =
154 parseModuleReferences(Keywords, AnnotatedLines);
156 if (References.empty())
161 InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
163 References = sortModuleReferences(References);
165 std::string ReferencesText;
166 for (
unsigned I = 0, E = References.size(); I != E; ++I) {
168 appendReference(ReferencesText, Reference);
171 ReferencesText +=
"\n";
174 if (!Reference.IsExport &&
175 (Reference.IsExport != References[I + 1].IsExport ||
176 Reference.Category != References[I + 1].Category)) {
177 ReferencesText +=
"\n";
181 llvm::StringRef PreviousText = getSourceText(InsertionPoint);
182 if (ReferencesText == PreviousText)
192 unsigned PreviousSize = PreviousText.size();
193 while (ReferencesText.size() < PreviousSize)
194 ReferencesText +=
" ";
198 !(FirstNonImportLine->
First->
is(tok::comment) &&
200 ReferencesText +=
"\n";
203 LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n"
204 << PreviousText <<
"\nwith:\n"
205 << ReferencesText <<
"\n");
212 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
225 StringRef FileContents;
227 void skipComments() { Current = skipComments(Current); }
229 FormatToken *skipComments(FormatToken *Tok) {
230 while (Tok && Tok->is(tok::comment))
236 Current = Current->Next;
238 if (!Current || Current == LineEnd->
Next) {
241 Current = &invalidToken;
245 StringRef getSourceText(SourceRange Range) {
246 return getSourceText(
Range.getBegin(),
Range.getEnd());
249 StringRef getSourceText(SourceLocation
Begin, SourceLocation End) {
251 return FileContents.substr(
SM.getFileOffset(
Begin),
252 SM.getFileOffset(End) -
SM.getFileOffset(
Begin));
259 SmallVector<JsModuleReference, 16>
260 sortModuleReferences(
const SmallVector<JsModuleReference, 16> &References) {
265 const auto *Start = References.begin();
266 SmallVector<JsModuleReference, 16> ReferencesSorted;
267 while (Start != References.end()) {
268 while (Start != References.end() && Start->FormattingOff) {
270 ReferencesSorted.push_back(*Start);
273 SmallVector<JsModuleReference, 16> SortChunk;
274 while (Start != References.end() && !Start->FormattingOff) {
276 SortChunk.push_back(*Start);
279 llvm::stable_sort(SortChunk);
280 mergeModuleReferences(SortChunk);
281 ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
284 return ReferencesSorted;
296 void mergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
297 if (References.empty())
299 JsModuleReference *PreviousReference = References.begin();
300 auto *
Reference = std::next(References.begin());
301 while (Reference != References.end()) {
309 Reference->IsExport != PreviousReference->IsExport ||
310 Reference->IsTypeOnly != PreviousReference->IsTypeOnly ||
311 !PreviousReference->Prefix.empty() || !
Reference->Prefix.empty() ||
312 !PreviousReference->DefaultImport.empty() ||
314 PreviousReference->URL !=
Reference->URL) {
320 PreviousReference->Symbols.append(
Reference->Symbols);
321 PreviousReference->SymbolsMerged =
true;
328 void appendReference(std::string &Buffer, JsModuleReference &Reference) {
336 SmallVector<JsImportedSymbol, 1> Symbols =
Reference.Symbols;
338 Symbols, [&](
const JsImportedSymbol &LHS,
const JsImportedSymbol &RHS) {
339 return LHS.Symbol.compare_insensitive(RHS.Symbol) < 0;
343 StringRef ReferenceStmt = getSourceText(
Reference.Range);
344 Buffer += ReferenceStmt;
350 if (!Symbols.empty()) {
351 Buffer += getSourceText(Symbols.front().Range);
352 for (
const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
354 Buffer += getSourceText(Symbol.Range);
364 std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
365 parseModuleReferences(
const AdditionalKeywords &Keywords,
366 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
367 SmallVector<JsModuleReference, 16> References;
368 SourceLocation Start;
369 AnnotatedLine *FirstNonImportLine =
nullptr;
370 bool AnyImportAffected =
false;
371 bool FormattingOff =
false;
372 for (
auto *Line : AnnotatedLines) {
374 Current = Line->First;
375 LineEnd = Line->Last;
378 while (Current && Current->is(tok::comment)) {
379 StringRef CommentText = Current->TokenText.trim();
381 FormattingOff =
true;
383 FormattingOff =
false;
388 if (!References.empty()) {
389 References.back().Range.setEnd(Current->Tok.getEndLoc());
390 Start = Current->Tok.getEndLoc().getLocWithOffset(1);
394 Current = Current->Next;
397 if (Start.isInvalid() || References.empty()) {
401 Start = Line->First->Tok.getLocation();
405 FirstNonImportLine = Line;
413 if (!parseModuleReference(Keywords, Reference)) {
414 if (!FirstNonImportLine)
415 FirstNonImportLine = Line;
418 FirstNonImportLine =
nullptr;
419 AnyImportAffected = AnyImportAffected || Line->Affected;
422 llvm::dbgs() <<
"JsModuleReference: {"
423 <<
"formatting_off: " <<
Reference.FormattingOff
428 for (
const JsImportedSymbol &Symbol :
Reference.Symbols)
429 llvm::dbgs() <<
", " << Symbol.Symbol <<
" as " << Symbol.Alias;
430 llvm::dbgs() <<
", text: " << getSourceText(
Reference.Range);
431 llvm::dbgs() <<
"}\n";
433 References.push_back(Reference);
434 Start = SourceLocation();
437 if (!AnyImportAffected)
439 return std::make_pair(References, FirstNonImportLine);
445 bool parseModuleReference(
const AdditionalKeywords &Keywords,
446 JsModuleReference &Reference) {
447 if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
449 Reference.IsExport = Current->is(tok::kw_export);
452 if (Current->isStringLiteral() && !
Reference.IsExport) {
456 Current->TokenText.substr(1, Current->TokenText.size() - 2);
460 if (!parseModuleBindings(Keywords, Reference))
463 if (Current->is(Keywords.kw_from)) {
466 if (!Current->isStringLiteral())
470 Current->TokenText.substr(1, Current->TokenText.size() - 2);
474 }
else if (
Reference.URL.startswith(
".")) {
483 bool parseModuleBindings(
const AdditionalKeywords &Keywords,
484 JsModuleReference &Reference) {
485 if (parseStarBinding(Keywords, Reference))
487 return parseNamedBindings(Keywords, Reference);
490 bool parseStarBinding(
const AdditionalKeywords &Keywords,
491 JsModuleReference &Reference) {
493 if (Current->is(Keywords.kw_type) && Current->Next &&
494 Current->Next->is(tok::star)) {
498 if (Current->isNot(tok::star))
501 if (Current->isNot(Keywords.kw_as))
504 if (Current->isNot(tok::identifier))
511 bool parseNamedBindings(
const AdditionalKeywords &Keywords,
512 JsModuleReference &Reference) {
513 if (Current->is(Keywords.kw_type) && Current->Next &&
514 Current->Next->isOneOf(tok::identifier, tok::l_brace)) {
520 if (!
Reference.IsExport && Current->is(tok::identifier)) {
521 Reference.DefaultImport = Current->TokenText;
523 if (Current->is(Keywords.kw_from))
526 if (Current->is(tok::equal)) {
529 while (Current->is(tok::identifier)) {
531 if (Current->is(tok::semi))
533 if (Current->isNot(tok::period))
538 if (Current->isNot(tok::comma))
542 if (Current->isNot(tok::l_brace))
546 Reference.SymbolsStart = Current->Tok.getEndLoc();
547 while (Current->isNot(tok::r_brace)) {
549 if (Current->is(tok::r_brace))
552 Current->is(Keywords.kw_type) && Current->Next &&
553 Current->Next->isOneOf(tok::identifier, tok::kw_default);
554 if (!isTypeOnly && !Current->isOneOf(tok::identifier, tok::kw_default))
557 JsImportedSymbol Symbol;
559 Symbol.Range.setBegin(
560 Current->getPreviousNonComment()->Next->WhitespaceRange.getBegin());
563 Symbol.Symbol = Current->TokenText;
566 if (Current->is(Keywords.kw_as)) {
568 if (!Current->isOneOf(tok::identifier, tok::kw_default))
570 Symbol.Alias = Current->TokenText;
573 Symbol.Range.setEnd(Current->Tok.getLocation());
576 if (!Current->isOneOf(tok::r_brace, tok::comma))
579 Reference.SymbolsEnd = Current->Tok.getLocation();
582 if (Current->Previous->is(tok::comma))
583 Reference.SymbolsEnd = Current->Previous->Tok.getLocation();
592 StringRef FileName) {
Defines the Diagnostic-related interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
This file implements a sorter for JavaScript ES6 imports.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
This file declares an abstract TokenAnalyzer, and associated helper classes.
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
A trivial tuple used to represent a source range.
void setEnd(SourceLocation e)
SourceLocation getEndLoc() const
void startToken()
Reset all flags to cleared.
@ Result
The result type of a method or function.