Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/HeaderIncludeGen.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- HeaderIncludeGen.cpp - Generate Header Includes -------------------===//
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
#include "clang/Frontend/DependencyOutputOptions.h"
10
#include "clang/Frontend/Utils.h"
11
#include "clang/Basic/SourceManager.h"
12
#include "clang/Frontend/FrontendDiagnostic.h"
13
#include "clang/Lex/Preprocessor.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/Support/raw_ostream.h"
16
using namespace clang;
17
18
namespace {
19
class HeaderIncludesCallback : public PPCallbacks {
20
  SourceManager &SM;
21
  raw_ostream *OutputFile;
22
  const DependencyOutputOptions &DepOpts;
23
  unsigned CurrentIncludeDepth;
24
  bool HasProcessedPredefines;
25
  bool OwnsOutputFile;
26
  bool ShowAllHeaders;
27
  bool ShowDepth;
28
  bool MSStyle;
29
30
public:
31
  HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
32
                         raw_ostream *OutputFile_,
33
                         const DependencyOutputOptions &DepOpts,
34
                         bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
35
      : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
36
        CurrentIncludeDepth(0), HasProcessedPredefines(false),
37
        OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
38
13
        ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
39
40
6
  ~HeaderIncludesCallback() override {
41
6
    if (OwnsOutputFile)
42
0
      delete OutputFile;
43
6
  }
44
45
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
46
                   SrcMgr::CharacteristicKind FileType,
47
                   FileID PrevFID) override;
48
};
49
}
50
51
static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
52
                            bool ShowDepth, unsigned CurrentIncludeDepth,
53
31
                            bool MSStyle) {
54
31
  // Write to a temporary string to avoid unnecessary flushing on errs().
55
31
  SmallString<512> Pathname(Filename);
56
31
  if (!MSStyle)
57
8
    Lexer::Stringify(Pathname);
58
31
59
31
  SmallString<256> Msg;
60
31
  if (MSStyle)
61
23
    Msg += "Note: including file:";
62
31
63
31
  if (ShowDepth) {
64
31
    // The main source file is at depth 1, so skip one dot.
65
69
    for (unsigned i = 1; i != CurrentIncludeDepth; 
++i38
)
66
38
      Msg += MSStyle ? 
' '29
:
'.'9
;
67
31
68
31
    if (!MSStyle)
69
8
      Msg += ' ';
70
31
  }
71
31
  Msg += Pathname;
72
31
  Msg += '\n';
73
31
74
31
  *OutputFile << Msg;
75
31
  OutputFile->flush();
76
31
}
77
78
void clang::AttachHeaderIncludeGen(Preprocessor &PP,
79
                                   const DependencyOutputOptions &DepOpts,
80
                                   bool ShowAllHeaders, StringRef OutputPath,
81
13
                                   bool ShowDepth, bool MSStyle) {
82
13
  raw_ostream *OutputFile = &llvm::errs();
83
13
  bool OwnsOutputFile = false;
84
13
85
13
  // Choose output stream, when printing in cl.exe /showIncludes style.
86
13
  if (MSStyle) {
87
10
    switch (DepOpts.ShowIncludesDest) {
88
10
    default:
89
0
      llvm_unreachable("Invalid destination for /showIncludes output!");
90
10
    case ShowIncludesDestination::Stderr:
91
1
      OutputFile = &llvm::errs();
92
1
      break;
93
10
    case ShowIncludesDestination::Stdout:
94
9
      OutputFile = &llvm::outs();
95
9
      break;
96
13
    }
97
13
  }
98
13
99
13
  // Open the output file, if used.
100
13
  if (!OutputPath.empty()) {
101
0
    std::error_code EC;
102
0
    llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
103
0
        OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
104
0
    if (EC) {
105
0
      PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
106
0
          << EC.message();
107
0
      delete OS;
108
0
    } else {
109
0
      OS->SetUnbuffered();
110
0
      OutputFile = OS;
111
0
      OwnsOutputFile = true;
112
0
    }
113
0
  }
114
13
115
13
  // Print header info for extra headers, pretending they were discovered by
116
13
  // the regular preprocessor. The primary use case is to support proper
117
13
  // generation of Make / Ninja file dependencies for implicit includes, such
118
13
  // as sanitizer blacklists. It's only important for cl.exe compatibility,
119
13
  // the GNU way to generate rules is -M / -MM / -MD / -MMD.
120
13
  for (const auto &Header : DepOpts.ExtraDeps)
121
1
    PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle);
122
13
  PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(
123
13
      &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
124
13
      MSStyle));
125
13
}
126
127
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
128
                                         FileChangeReason Reason,
129
                                       SrcMgr::CharacteristicKind NewFileType,
130
126
                                       FileID PrevFID) {
131
126
  // Unless we are exiting a #include, make sure to skip ahead to the line the
132
126
  // #include directive was at.
133
126
  PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
134
126
  if (UserLoc.isInvalid())
135
0
    return;
136
126
137
126
  // Adjust the current include depth.
138
126
  if (Reason == PPCallbacks::EnterFile) {
139
66
    ++CurrentIncludeDepth;
140
66
  } else 
if (60
Reason == PPCallbacks::ExitFile60
) {
141
51
    if (CurrentIncludeDepth)
142
51
      --CurrentIncludeDepth;
143
51
144
51
    // We track when we are done with the predefines by watching for the first
145
51
    // place where we drop back to a nesting depth of 1.
146
51
    if (CurrentIncludeDepth == 1 && 
!HasProcessedPredefines26
) {
147
11
      if (!DepOpts.ShowIncludesPretendHeader.empty()) {
148
0
        PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader,
149
0
                        ShowDepth, 2, MSStyle);
150
0
      }
151
11
      HasProcessedPredefines = true;
152
11
    }
153
51
154
51
    return;
155
51
  } else
156
9
    return;
157
66
158
66
  // Show the header if we are (a) past the predefines, or (b) showing all
159
66
  // headers and in the predefines at a depth past the initial file and command
160
66
  // line buffers.
161
66
  bool ShowHeader = (HasProcessedPredefines ||
162
66
                     
(47
ShowAllHeaders47
&&
CurrentIncludeDepth > 237
));
163
66
  unsigned IncludeDepth = CurrentIncludeDepth;
164
66
  if (!HasProcessedPredefines)
165
47
    --IncludeDepth; // Ignore indent from <built-in>.
166
19
  else if (!DepOpts.ShowIncludesPretendHeader.empty())
167
0
    ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
168
66
169
66
  // Dump the header include information we are past the predefines buffer or
170
66
  // are showing all headers and this isn't the magic implicit <command line>
171
66
  // header.
172
66
  // FIXME: Identify headers in a more robust way than comparing their name to
173
66
  // "<command line>" and "<built-in>" in a bunch of places.
174
66
  if (ShowHeader && 
Reason == PPCallbacks::EnterFile36
&&
175
66
      
UserLoc.getFilename() != StringRef("<command line>")36
) {
176
30
    PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
177
30
                    MSStyle);
178
30
  }
179
66
}