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;
117 if (LHS.
URL.empty() != RHS.
URL.empty())
118 return LHS.
URL.empty() < RHS.
URL.empty();
119 if (
int Res = LHS.
URL.compare_insensitive(RHS.
URL))
136 FileContents(
Env.getSourceManager().getBufferData(
Env.getFileID())) {
141 std::pair<tooling::Replacements, unsigned>
151 std::tie(References, FirstNonImportLine) =
152 parseModuleReferences(Keywords, AnnotatedLines);
154 if (References.empty())
159 InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
161 References = sortModuleReferences(References);
163 std::string ReferencesText;
164 for (
unsigned I = 0,
E = References.size(); I !=
E; ++I) {
166 appendReference(ReferencesText,
Reference);
169 ReferencesText +=
"\n";
173 (
Reference.IsExport != References[I + 1].IsExport ||
174 Reference.Category != References[I + 1].Category)) {
175 ReferencesText +=
"\n";
179 StringRef PreviousText = getSourceText(InsertionPoint);
180 if (ReferencesText == PreviousText)
190 unsigned PreviousSize = PreviousText.size();
191 while (ReferencesText.size() < PreviousSize)
192 ReferencesText +=
" ";
196 !(FirstNonImportLine->
First->
is(tok::comment) &&
198 ReferencesText +=
"\n";
201 LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n"
202 << PreviousText <<
"\nwith:\n"
203 << ReferencesText <<
"\n");
210 llvm::errs() <<
toString(std::move(Err)) <<
"\n";
223 StringRef FileContents;
225 void skipComments() { Current = skipComments(Current); }
227 FormatToken *skipComments(FormatToken *Tok) {
228 while (Tok && Tok->is(tok::comment))
234 Current = Current->Next;
236 if (!Current || Current == LineEnd->
Next) {
239 Current = &invalidToken;
243 StringRef getSourceText(SourceRange
Range) {
244 return getSourceText(
Range.getBegin(),
Range.getEnd());
247 StringRef getSourceText(SourceLocation
Begin, SourceLocation End) {
249 return FileContents.substr(
SM.getFileOffset(
Begin),
250 SM.getFileOffset(End) -
SM.getFileOffset(
Begin));
257 SmallVector<JsModuleReference, 16>
258 sortModuleReferences(
const SmallVector<JsModuleReference, 16> &References) {
263 const auto *Start = References.begin();
264 SmallVector<JsModuleReference, 16> ReferencesSorted;
265 while (Start != References.end()) {
266 while (Start != References.end() && Start->FormattingOff) {
268 ReferencesSorted.push_back(*Start);
271 SmallVector<JsModuleReference, 16> SortChunk;
272 while (Start != References.end() && !Start->FormattingOff) {
274 SortChunk.push_back(*Start);
277 stable_sort(SortChunk);
278 mergeModuleReferences(SortChunk);
279 ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
282 return ReferencesSorted;
294 void mergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
295 if (References.empty())
297 JsModuleReference *PreviousReference = References.begin();
298 auto *
Reference = std::next(References.begin());
307 Reference->IsExport != PreviousReference->IsExport ||
308 Reference->IsTypeOnly != PreviousReference->IsTypeOnly ||
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 [&](
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 : 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";
432 Start = SourceLocation();
435 if (!AnyImportAffected)
437 return std::make_pair(References, FirstNonImportLine);
443 bool parseModuleReference(
const AdditionalKeywords &Keywords,
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.starts_with(
".")) {
481 bool parseModuleBindings(
const AdditionalKeywords &Keywords,
483 if (parseStarBinding(Keywords,
Reference))
485 return parseNamedBindings(Keywords,
Reference);
488 bool parseStarBinding(
const AdditionalKeywords &Keywords,
491 if (Current->is(Keywords.kw_type) && Current->Next &&
492 Current->Next->is(tok::star)) {
496 if (Current->isNot(tok::star))
499 if (Current->isNot(Keywords.kw_as))
502 if (Current->isNot(tok::identifier))
509 bool parseNamedBindings(
const AdditionalKeywords &Keywords,
511 if (Current->is(Keywords.kw_type) && Current->Next &&
512 Current->Next->isOneOf(tok::identifier, tok::l_brace)) {
518 if (!
Reference.IsExport && Current->is(tok::identifier)) {
519 Reference.DefaultImport = Current->TokenText;
521 if (Current->is(Keywords.kw_from))
524 if (Current->is(tok::equal)) {
527 while (Current->is(tok::identifier)) {
529 if (Current->is(tok::semi))
531 if (Current->isNot(tok::period))
536 if (Current->isNot(tok::comma))
540 if (Current->isNot(tok::l_brace))
544 Reference.SymbolsStart = Current->Tok.getEndLoc();
545 while (Current->isNot(tok::r_brace)) {
547 if (Current->is(tok::r_brace))
549 auto IsIdentifier = [](
const auto *Tok) {
550 return Tok->isOneOf(tok::identifier, tok::kw_default, tok::kw_template);
552 bool isTypeOnly = Current->is(Keywords.kw_type) && Current->Next &&
553 IsIdentifier(Current->Next);
554 if (!isTypeOnly && !IsIdentifier(Current))
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 (!IsIdentifier(Current))
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();
Defines the Diagnostic-related interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
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.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.