Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/TokenAnalyzer.cpp
Line
Count
Source (jump to first uncovered line)
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
41.1k
      NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {
43
41.1k
  SourceLocation StartOfFile = SM.getLocForStartOfFile(ID);
44
41.7k
  for (const tooling::Range &Range : Ranges) {
45
41.7k
    SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
46
41.7k
    SourceLocation End = Start.getLocWithOffset(Range.getLength());
47
41.7k
    CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
48
41.7k
  }
49
41.1k
}
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
41.1k
          Env.getSourceManager().getBufferData(Env.getFileID()))) {
57
41.1k
  LLVM_DEBUG(
58
41.1k
      llvm::dbgs() << "File encoding: "
59
41.1k
                   << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
60
41.1k
                   << "\n");
61
41.1k
  LLVM_DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
62
41.1k
                          << "\n");
63
41.1k
}
64
65
41.1k
std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process() {
66
41.1k
  tooling::Replacements Result;
67
41.1k
  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
68
41.1k
  IdentifierTable IdentTable(getFormattingLangOpts(Style));
69
41.1k
  FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
70
41.1k
                       Env.getFirstStartColumn(), Style, Encoding, Allocator,
71
72
41.1k
                       IdentTable);
73
41.1k
  ArrayRef<FormatToken *> Toks(Lex.lex());
74
41.1k
  SmallVector<FormatToken *, 10> Tokens(Toks.begin(), Toks.end());
75
41.1k
  UnwrappedLineParser Parser(Style, Lex.getKeywords(),
76
41.1k
                             Env.getFirstStartColumn(), Tokens, *this);
77
41.1k
  Parser.parse();
78
41.1k
  assert(UnwrappedLines.rbegin()->empty());
79
0
  unsigned Penalty = 0;
80
82.6k
  for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; 
++Run41.4k
) {
81
41.4k
    LLVM_DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
82
41.4k
    SmallVector<AnnotatedLine *, 16> AnnotatedLines;
83
84
41.4k
    TokenAnnotator Annotator(Style, Lex.getKeywords());
85
206k
    for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; 
++i165k
) {
86
165k
      AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
87
165k
      Annotator.annotate(*AnnotatedLines.back());
88
165k
    }
89
90
41.4k
    std::pair<tooling::Replacements, unsigned> RunResult =
91
41.4k
        analyze(Annotator, AnnotatedLines, Lex);
92
93
41.4k
    LLVM_DEBUG({
94
41.4k
      llvm::dbgs() << "Replacements for run " << Run << ":\n";
95
41.4k
      for (tooling::Replacements::const_iterator I = RunResult.first.begin(),
96
41.4k
                                                 E = RunResult.first.end();
97
41.4k
           I != E; ++I) {
98
41.4k
        llvm::dbgs() << I->toString() << "\n";
99
41.4k
      }
100
41.4k
    });
101
206k
    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; 
++i165k
) {
102
165k
      delete AnnotatedLines[i];
103
165k
    }
104
105
41.4k
    Penalty += RunResult.second;
106
41.4k
    for (const auto &R : RunResult.first) {
107
25.9k
      auto Err = Result.add(R);
108
      // FIXME: better error handling here. For now, simply return an empty
109
      // Replacements to indicate failure.
110
25.9k
      if (Err) {
111
0
        llvm::errs() << llvm::toString(std::move(Err)) << "\n";
112
0
        return {tooling::Replacements(), 0};
113
0
      }
114
25.9k
    }
115
41.4k
  }
116
41.1k
  return {Result, Penalty};
117
41.1k
}
118
119
165k
void TokenAnalyzer::consumeUnwrappedLine(const UnwrappedLine &TheLine) {
120
165k
  assert(!UnwrappedLines.empty());
121
0
  UnwrappedLines.back().push_back(TheLine);
122
165k
}
123
124
41.4k
void TokenAnalyzer::finishRun() {
125
41.4k
  UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
126
41.4k
}
127
128
} // end namespace format
129
} // end namespace clang