Coverage Report

Created: 2022-05-17 06:19

/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/ADT/SmallVector.h"
30
#include "llvm/Support/Debug.h"
31
#include <type_traits>
32
33
#define DEBUG_TYPE "format-formatter"
34
35
namespace clang {
36
namespace format {
37
38
// FIXME: Instead of printing the diagnostic we should store it and have a
39
// better way to return errors through the format APIs.
40
class FatalDiagnosticConsumer : public DiagnosticConsumer {
41
public:
42
  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
43
0
                        const Diagnostic &Info) override {
44
0
    if (DiagLevel == DiagnosticsEngine::Fatal) {
45
0
      Fatal = true;
46
0
      llvm::SmallVector<char, 128> Message;
47
0
      Info.FormatDiagnostic(Message);
48
0
      llvm::errs() << Message << "\n";
49
0
    }
50
0
  }
51
52
63.9k
  bool fatalError() const { return Fatal; }
53
54
private:
55
  bool Fatal = false;
56
};
57
58
std::unique_ptr<Environment>
59
Environment::make(StringRef Code, StringRef FileName,
60
                  ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
61
63.9k
                  unsigned NextStartColumn, unsigned LastStartColumn) {
62
63.9k
  auto Env = std::make_unique<Environment>(Code, FileName, FirstStartColumn,
63
63.9k
                                           NextStartColumn, LastStartColumn);
64
63.9k
  FatalDiagnosticConsumer Diags;
65
63.9k
  Env->SM.getDiagnostics().setClient(&Diags, /*ShouldOwnClient=*/false);
66
63.9k
  SourceLocation StartOfFile = Env->SM.getLocForStartOfFile(Env->ID);
67
64.7k
  for (const tooling::Range &Range : Ranges) {
68
64.7k
    SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
69
64.7k
    SourceLocation End = Start.getLocWithOffset(Range.getLength());
70
64.7k
    Env->CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
71
64.7k
  }
72
  // Validate that we can get the buffer data without a fatal error.
73
63.9k
  Env->SM.getBufferData(Env->ID);
74
63.9k
  if (Diags.fatalError())
75
0
    return nullptr;
76
63.9k
  return Env;
77
63.9k
}
78
79
Environment::Environment(StringRef Code, StringRef FileName,
80
                         unsigned FirstStartColumn, unsigned NextStartColumn,
81
                         unsigned LastStartColumn)
82
    : VirtualSM(new SourceManagerForFile(FileName, Code)), SM(VirtualSM->get()),
83
      ID(VirtualSM->get().getMainFileID()), FirstStartColumn(FirstStartColumn),
84
64.2k
      NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {}
85
86
TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
87
    : Style(Style), Env(Env),
88
      AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
89
      UnwrappedLines(1),
90
      Encoding(encoding::detectEncoding(
91
64.2k
          Env.getSourceManager().getBufferData(Env.getFileID()))) {
92
64.2k
  LLVM_DEBUG(
93
64.2k
      llvm::dbgs() << "File encoding: "
94
64.2k
                   << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
95
64.2k
                   << "\n");
96
64.2k
  LLVM_DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
97
64.2k
                          << "\n");
98
64.2k
}
99
100
64.2k
std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process() {
101
64.2k
  tooling::Replacements Result;
102
64.2k
  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
103
64.2k
  IdentifierTable IdentTable(getFormattingLangOpts(Style));
104
64.2k
  FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
105
64.2k
                       Env.getFirstStartColumn(), Style, Encoding, Allocator,
106
107
64.2k
                       IdentTable);
108
64.2k
  ArrayRef<FormatToken *> Toks(Lex.lex());
109
64.2k
  SmallVector<FormatToken *, 10> Tokens(Toks.begin(), Toks.end());
110
64.2k
  UnwrappedLineParser Parser(Style, Lex.getKeywords(),
111
64.2k
                             Env.getFirstStartColumn(), Tokens, *this);
112
64.2k
  Parser.parse();
113
64.2k
  assert(UnwrappedLines.back().empty());
114
0
  unsigned Penalty = 0;
115
129k
  for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; 
++Run64.9k
) {
116
64.9k
    const auto &Lines = UnwrappedLines[Run];
117
64.9k
    LLVM_DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
118
64.9k
    SmallVector<AnnotatedLine *, 16> AnnotatedLines;
119
64.9k
    AnnotatedLines.reserve(Lines.size());
120
121
64.9k
    TokenAnnotator Annotator(Style, Lex.getKeywords());
122
243k
    for (const UnwrappedLine &Line : Lines) {
123
243k
      AnnotatedLines.push_back(new AnnotatedLine(Line));
124
243k
      Annotator.annotate(*AnnotatedLines.back());
125
243k
    }
126
127
64.9k
    std::pair<tooling::Replacements, unsigned> RunResult =
128
64.9k
        analyze(Annotator, AnnotatedLines, Lex);
129
130
64.9k
    LLVM_DEBUG({
131
64.9k
      llvm::dbgs() << "Replacements for run " << Run << ":\n";
132
64.9k
      for (const tooling::Replacement &Fix : RunResult.first)
133
64.9k
        llvm::dbgs() << Fix.toString() << "\n";
134
64.9k
    });
135
64.9k
    for (AnnotatedLine *Line : AnnotatedLines)
136
243k
      delete Line;
137
138
64.9k
    Penalty += RunResult.second;
139
64.9k
    for (const auto &R : RunResult.first) {
140
31.1k
      auto Err = Result.add(R);
141
      // FIXME: better error handling here. For now, simply return an empty
142
      // Replacements to indicate failure.
143
31.1k
      if (Err) {
144
0
        llvm::errs() << llvm::toString(std::move(Err)) << "\n";
145
0
        return {tooling::Replacements(), 0};
146
0
      }
147
31.1k
    }
148
64.9k
  }
149
64.2k
  return {Result, Penalty};
150
64.2k
}
151
152
243k
void TokenAnalyzer::consumeUnwrappedLine(const UnwrappedLine &TheLine) {
153
243k
  assert(!UnwrappedLines.empty());
154
0
  UnwrappedLines.back().push_back(TheLine);
155
243k
}
156
157
64.9k
void TokenAnalyzer::finishRun() {
158
64.9k
  UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
159
64.9k
}
160
161
} // end namespace format
162
} // end namespace clang