1 //===--- TokenAnalyzer.cpp - Analyze Token Streams --------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file implements an abstract TokenAnalyzer and associated helper 11 /// classes. TokenAnalyzer can be extended to generate replacements based on 12 /// an annotated and pre-processed token stream. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "TokenAnalyzer.h" 17 #include "AffectedRangeManager.h" 18 #include "Encoding.h" 19 #include "FormatToken.h" 20 #include "FormatTokenLexer.h" 21 #include "TokenAnnotator.h" 22 #include "UnwrappedLineParser.h" 23 #include "clang/Basic/Diagnostic.h" 24 #include "clang/Basic/DiagnosticOptions.h" 25 #include "clang/Basic/FileManager.h" 26 #include "clang/Basic/SourceManager.h" 27 #include "clang/Format/Format.h" 28 #include "llvm/ADT/STLExtras.h" 29 #include "llvm/Support/Debug.h" 30 31 #define DEBUG_TYPE "format-formatter" 32 33 namespace clang { 34 namespace format { 35 36 Environment::Environment(StringRef Code, StringRef FileName, 37 ArrayRef<tooling::Range> Ranges, 38 unsigned FirstStartColumn, unsigned NextStartColumn, 39 unsigned LastStartColumn) 40 : VirtualSM(new SourceManagerForFile(FileName, Code)), SM(VirtualSM->get()), 41 ID(VirtualSM->get().getMainFileID()), FirstStartColumn(FirstStartColumn), 42 NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) { 43 SourceLocation StartOfFile = SM.getLocForStartOfFile(ID); 44 for (const tooling::Range &Range : Ranges) { 45 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset()); 46 SourceLocation End = Start.getLocWithOffset(Range.getLength()); 47 CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); 48 } 49 } 50 51 TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style) 52 : Style(Style), Env(Env), 53 AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()), 54 UnwrappedLines(1), 55 Encoding(encoding::detectEncoding( 56 Env.getSourceManager().getBufferData(Env.getFileID()))) { 57 LLVM_DEBUG( 58 llvm::dbgs() << "File encoding: " 59 << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown") 60 << "\n"); 61 LLVM_DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) 62 << "\n"); 63 } 64 65 std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process() { 66 tooling::Replacements Result; 67 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 68 IdentifierTable IdentTable(getFormattingLangOpts(Style)); 69 FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(), 70 Env.getFirstStartColumn(), Style, Encoding, Allocator, 71 72 IdentTable); 73 ArrayRef<FormatToken *> Toks(Lex.lex()); 74 SmallVector<FormatToken *, 10> Tokens(Toks.begin(), Toks.end()); 75 UnwrappedLineParser Parser(Style, Lex.getKeywords(), 76 Env.getFirstStartColumn(), Tokens, *this); 77 Parser.parse(); 78 assert(UnwrappedLines.rbegin()->empty()); 79 unsigned Penalty = 0; 80 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; ++Run) { 81 LLVM_DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); 82 SmallVector<AnnotatedLine *, 16> AnnotatedLines; 83 84 TokenAnnotator Annotator(Style, Lex.getKeywords()); 85 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { 86 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); 87 Annotator.annotate(*AnnotatedLines.back()); 88 } 89 90 std::pair<tooling::Replacements, unsigned> RunResult = 91 analyze(Annotator, AnnotatedLines, Lex); 92 93 LLVM_DEBUG({ 94 llvm::dbgs() << "Replacements for run " << Run << ":\n"; 95 for (tooling::Replacements::const_iterator I = RunResult.first.begin(), 96 E = RunResult.first.end(); 97 I != E; ++I) { 98 llvm::dbgs() << I->toString() << "\n"; 99 } 100 }); 101 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 102 delete AnnotatedLines[i]; 103 } 104 105 Penalty += RunResult.second; 106 for (const auto &R : RunResult.first) { 107 auto Err = Result.add(R); 108 // FIXME: better error handling here. For now, simply return an empty 109 // Replacements to indicate failure. 110 if (Err) { 111 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 112 return {tooling::Replacements(), 0}; 113 } 114 } 115 } 116 return {Result, Penalty}; 117 } 118 119 void TokenAnalyzer::consumeUnwrappedLine(const UnwrappedLine &TheLine) { 120 assert(!UnwrappedLines.empty()); 121 UnwrappedLines.back().push_back(TheLine); 122 } 123 124 void TokenAnalyzer::finishRun() { 125 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); 126 } 127 128 } // end namespace format 129 } // end namespace clang 130