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;
118 if (LHS.
URL.empty() != RHS.
URL.empty())
119 return LHS.
URL.empty() < RHS.
URL.empty();
120 if (
int Res = LHS.
URL.compare_insensitive(RHS.
URL))
137 FileContents(
Env.getSourceManager().getBufferData(
Env.getFileID())) {
142 std::pair<tooling::Replacements, unsigned>
152 std::tie(References, FirstNonImportLine) =
153 parseModuleReferences(Keywords, AnnotatedLines);
155 if (References.empty())
160 InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
162 References = sortModuleReferences(References);
164 std::string ReferencesText;
165 for (
unsigned I = 0, E = References.size(); I != E; ++I) {
167 appendReference(ReferencesText, Reference);
170 ReferencesText +=
"\n";
173 if (!Reference.IsExport &&
174 (Reference.IsExport != References[I + 1].IsExport ||
175 Reference.Category != References[I + 1].Category)) {
176 ReferencesText +=
"\n";
180 llvm::StringRef PreviousText = getSourceText(InsertionPoint);
181 if (ReferencesText == PreviousText)
191 unsigned PreviousSize = PreviousText.size();
192 while (ReferencesText.size() < PreviousSize)
193 ReferencesText +=
" ";
197 !(FirstNonImportLine->
First->
is(tok::comment) &&
199 ReferencesText +=
"\n";
202 LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n"
203 << PreviousText <<
"\nwith:\n"
204 << ReferencesText <<
"\n");
211 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
224 StringRef FileContents;
226 void skipComments() { Current = skipComments(Current); }
228 FormatToken *skipComments(FormatToken *Tok) {
229 while (Tok && Tok->is(tok::comment))
235 Current = Current->Next;
237 if (!Current || Current == LineEnd->
Next) {
240 Current = &invalidToken;
244 StringRef getSourceText(SourceRange Range) {
245 return getSourceText(
Range.getBegin(),
Range.getEnd());
248 StringRef getSourceText(SourceLocation
Begin, SourceLocation End) {
250 return FileContents.substr(
SM.getFileOffset(
Begin),
251 SM.getFileOffset(End) -
SM.getFileOffset(
Begin));
258 SmallVector<JsModuleReference, 16>
259 sortModuleReferences(
const SmallVector<JsModuleReference, 16> &References) {
264 const auto *Start = References.begin();
265 SmallVector<JsModuleReference, 16> ReferencesSorted;
266 while (Start != References.end()) {
267 while (Start != References.end() && Start->FormattingOff) {
269 ReferencesSorted.push_back(*Start);
272 SmallVector<JsModuleReference, 16> SortChunk;
273 while (Start != References.end() && !Start->FormattingOff) {
275 SortChunk.push_back(*Start);
278 llvm::stable_sort(SortChunk);
279 mergeModuleReferences(SortChunk);
280 ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
283 return ReferencesSorted;
295 void mergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
296 if (References.empty())
298 JsModuleReference *PreviousReference = References.begin();
299 auto *
Reference = std::next(References.begin());
300 while (Reference != References.end()) {
308 Reference->IsExport != PreviousReference->IsExport ||
309 !PreviousReference->Prefix.empty() || !
Reference->Prefix.empty() ||
310 !PreviousReference->DefaultImport.empty() ||
312 PreviousReference->URL !=
Reference->URL) {
318 PreviousReference->Symbols.append(
Reference->Symbols);
319 PreviousReference->SymbolsMerged =
true;
326 void appendReference(std::string &Buffer, JsModuleReference &Reference) {
334 SmallVector<JsImportedSymbol, 1> Symbols =
Reference.Symbols;
336 Symbols, [&](
const JsImportedSymbol &LHS,
const JsImportedSymbol &RHS) {
337 return LHS.Symbol.compare_insensitive(RHS.Symbol) < 0;
341 StringRef ReferenceStmt = getSourceText(
Reference.Range);
342 Buffer += ReferenceStmt;
348 if (!Symbols.empty()) {
349 Buffer += getSourceText(Symbols.front().Range);
350 for (
const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
352 Buffer += getSourceText(Symbol.Range);
362 std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
363 parseModuleReferences(
const AdditionalKeywords &Keywords,
364 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
365 SmallVector<JsModuleReference, 16> References;
366 SourceLocation Start;
367 AnnotatedLine *FirstNonImportLine =
nullptr;
368 bool AnyImportAffected =
false;
369 bool FormattingOff =
false;
370 for (
auto *Line : AnnotatedLines) {
372 Current = Line->First;
373 LineEnd = Line->Last;
376 while (Current && Current->is(tok::comment)) {
377 StringRef CommentText = Current->TokenText.trim();
379 FormattingOff =
true;
381 FormattingOff =
false;
386 if (!References.empty()) {
387 References.back().Range.setEnd(Current->Tok.getEndLoc());
388 Start = Current->Tok.getEndLoc().getLocWithOffset(1);
392 Current = Current->Next;
395 if (Start.isInvalid() || References.empty()) {
399 Start = Line->First->Tok.getLocation();
403 FirstNonImportLine = Line;
411 if (!parseModuleReference(Keywords, Reference)) {
412 if (!FirstNonImportLine)
413 FirstNonImportLine = Line;
416 FirstNonImportLine =
nullptr;
417 AnyImportAffected = AnyImportAffected || Line->Affected;
420 llvm::dbgs() <<
"JsModuleReference: {"
421 <<
"formatting_off: " <<
Reference.FormattingOff
426 for (
const JsImportedSymbol &Symbol :
Reference.Symbols)
427 llvm::dbgs() <<
", " << Symbol.Symbol <<
" as " << Symbol.Alias;
428 llvm::dbgs() <<
", text: " << getSourceText(
Reference.Range);
429 llvm::dbgs() <<
"}\n";
431 References.push_back(Reference);
432 Start = SourceLocation();
435 if (!AnyImportAffected)
437 return std::make_pair(References, FirstNonImportLine);
443 bool parseModuleReference(
const AdditionalKeywords &Keywords,
444 JsModuleReference &Reference) {
445 if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
447 Reference.IsExport = Current->is(tok::kw_export);
450 if (Current->isStringLiteral() && !
Reference.IsExport) {
454 Current->TokenText.substr(1, Current->TokenText.size() - 2);
458 if (!parseModuleBindings(Keywords, Reference))
461 if (Current->is(Keywords.kw_from)) {
464 if (!Current->isStringLiteral())
468 Current->TokenText.substr(1, Current->TokenText.size() - 2);
472 }
else if (
Reference.URL.startswith(
".")) {
481 bool parseModuleBindings(
const AdditionalKeywords &Keywords,
482 JsModuleReference &Reference) {
483 if (parseStarBinding(Keywords, Reference))
485 return parseNamedBindings(Keywords, Reference);
488 bool parseStarBinding(
const AdditionalKeywords &Keywords,
489 JsModuleReference &Reference) {
491 if (Current->isNot(tok::star))
494 if (Current->isNot(Keywords.kw_as))
497 if (Current->isNot(tok::identifier))
504 bool parseNamedBindings(
const AdditionalKeywords &Keywords,
505 JsModuleReference &Reference) {
507 if (Current->is(tok::identifier)) {
508 Reference.DefaultImport = Current->TokenText;
510 if (Current->is(Keywords.kw_from))
513 if (Current->is(tok::equal)) {
516 while (Current->is(tok::identifier)) {
518 if (Current->is(tok::semi))
520 if (!Current->is(tok::period))
525 if (Current->isNot(tok::comma))
529 if (Current->isNot(tok::l_brace))
533 Reference.SymbolsStart = Current->Tok.getEndLoc();
534 while (Current->isNot(tok::r_brace)) {
536 if (Current->is(tok::r_brace))
538 if (!Current->isOneOf(tok::identifier, tok::kw_default))
541 JsImportedSymbol Symbol;
542 Symbol.Symbol = Current->TokenText;
544 Symbol.Range.setBegin(
545 Current->getPreviousNonComment()->Next->WhitespaceRange.getBegin());
548 if (Current->is(Keywords.kw_as)) {
550 if (!Current->isOneOf(tok::identifier, tok::kw_default))
552 Symbol.Alias = Current->TokenText;
555 Symbol.Range.setEnd(Current->Tok.getLocation());
558 if (!Current->isOneOf(tok::r_brace, tok::comma))
561 Reference.SymbolsEnd = Current->Tok.getLocation();
564 if (Current->Previous->is(tok::comma))
565 Reference.SymbolsEnd = Current->Previous->Tok.getLocation();
574 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.
@ Reference
An optional reference should be skipped past.
@ Result
The result type of a method or function.