Coverage Report

Created: 2020-02-25 14:32

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