Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/Rewrite/HTMLPrint.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
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
// Pretty-printing of source code to HTML.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ASTConsumer.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/Basic/Diagnostic.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/SourceManager.h"
19
#include "clang/Lex/Preprocessor.h"
20
#include "clang/Rewrite/Core/HTMLRewrite.h"
21
#include "clang/Rewrite/Core/Rewriter.h"
22
#include "clang/Rewrite/Frontend/ASTConsumers.h"
23
#include "llvm/Support/raw_ostream.h"
24
using namespace clang;
25
26
//===----------------------------------------------------------------------===//
27
// Functional HTML pretty-printing.
28
//===----------------------------------------------------------------------===//
29
30
namespace {
31
  class HTMLPrinter : public ASTConsumer {
32
    Rewriter R;
33
    std::unique_ptr<raw_ostream> Out;
34
    Preprocessor &PP;
35
    bool SyntaxHighlight, HighlightMacros;
36
37
  public:
38
    HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
39
                bool _SyntaxHighlight, bool _HighlightMacros)
40
      : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
41
3
        HighlightMacros(_HighlightMacros) {}
42
43
    void Initialize(ASTContext &context) override;
44
    void HandleTranslationUnit(ASTContext &Ctx) override;
45
  };
46
}
47
48
std::unique_ptr<ASTConsumer>
49
clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
50
3
                         bool SyntaxHighlight, bool HighlightMacros) {
51
3
  return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
52
3
                                        HighlightMacros);
53
3
}
54
55
3
void HTMLPrinter::Initialize(ASTContext &context) {
56
3
  R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
57
3
}
58
59
3
void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
60
3
  if (PP.getDiagnostics().hasErrorOccurred())
61
0
    return;
62
3
63
3
  // Format the file.
64
3
  FileID FID = R.getSourceMgr().getMainFileID();
65
3
  const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
66
3
  StringRef Name;
67
3
  // In some cases, in particular the case where the input is from stdin,
68
3
  // there is no entry.  Fall back to the memory buffer for a name in those
69
3
  // cases.
70
3
  if (Entry)
71
3
    Name = Entry->getName();
72
0
  else
73
0
    Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
74
3
75
3
  html::AddLineNumbers(R, FID);
76
3
  html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
77
3
78
3
  // If we have a preprocessor, relex the file and syntax highlight.
79
3
  // We might not have a preprocessor if we come from a deserialized AST file,
80
3
  // for example.
81
3
82
3
  if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
83
3
  if (HighlightMacros) html::HighlightMacros(R, FID, PP);
84
3
  html::EscapeText(R, FID, false, true);
85
3
86
3
  // Emit the HTML.
87
3
  const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
88
3
  std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
89
3
  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
90
3
  Out->write(Buffer.get(), RewriteBuf.size());
91
3
}