Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/DependencyFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- DependencyFile.cpp - Generate dependency file --------------------===//
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
// This code generates dependency files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Frontend/Utils.h"
14
#include "clang/Basic/FileManager.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Frontend/DependencyOutputOptions.h"
17
#include "clang/Frontend/FrontendDiagnostic.h"
18
#include "clang/Lex/DirectoryLookup.h"
19
#include "clang/Lex/ModuleMap.h"
20
#include "clang/Lex/PPCallbacks.h"
21
#include "clang/Lex/Preprocessor.h"
22
#include "clang/Serialization/ASTReader.h"
23
#include "llvm/ADT/StringSet.h"
24
#include "llvm/ADT/StringSwitch.h"
25
#include "llvm/Support/FileSystem.h"
26
#include "llvm/Support/Path.h"
27
#include "llvm/Support/raw_ostream.h"
28
29
using namespace clang;
30
31
namespace {
32
struct DepCollectorPPCallbacks : public PPCallbacks {
33
  DependencyCollector &DepCollector;
34
  SourceManager &SM;
35
  DiagnosticsEngine &Diags;
36
  DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM,
37
                          DiagnosticsEngine &Diags)
38
3.24k
      : DepCollector(L), SM(SM), Diags(Diags) {}
39
40
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
41
                   SrcMgr::CharacteristicKind FileType,
42
861k
                   FileID PrevFID) override {
43
861k
    if (Reason != PPCallbacks::EnterFile)
44
443k
      return;
45
46
    // Dependency generation really does want to go all the way to the
47
    // file entry for a source location to find out what is depended on.
48
    // We do not want #line markers to affect dependency generation!
49
417k
    if (Optional<StringRef> Filename = SM.getNonBuiltinFilenameForID(
50
411k
            SM.getFileID(SM.getExpansionLoc(Loc))))
51
411k
      DepCollector.maybeAddDependency(
52
411k
          llvm::sys::path::remove_leading_dotslash(*Filename),
53
411k
          /*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false,
54
411k
          /*IsMissing*/ false);
55
417k
  }
56
57
  void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
58
696k
                   SrcMgr::CharacteristicKind FileType) override {
59
696k
    StringRef Filename =
60
696k
        llvm::sys::path::remove_leading_dotslash(SkippedFile.getName());
61
696k
    DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
62
696k
                                    /*IsSystem=*/isSystem(FileType),
63
696k
                                    /*IsModuleFile=*/false,
64
696k
                                    /*IsMissing=*/false);
65
696k
  }
66
67
  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
68
                          StringRef FileName, bool IsAngled,
69
                          CharSourceRange FilenameRange, const FileEntry *File,
70
                          StringRef SearchPath, StringRef RelativePath,
71
                          const Module *Imported,
72
1.10M
                          SrcMgr::CharacteristicKind FileType) override {
73
1.10M
    if (!File)
74
21
      DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
75
21
                                     /*IsSystem*/false, /*IsModuleFile*/false,
76
21
                                     /*IsMissing*/true);
77
    // Files that actually exist are handled by FileChanged.
78
1.10M
  }
79
80
  void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
81
                  Optional<FileEntryRef> File,
82
14.0k
                  SrcMgr::CharacteristicKind FileType) override {
83
14.0k
    if (!File)
84
4.94k
      return;
85
9.12k
    StringRef Filename =
86
9.12k
        llvm::sys::path::remove_leading_dotslash(File->getName());
87
9.12k
    DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
88
9.12k
                                    /*IsSystem=*/isSystem(FileType),
89
9.12k
                                    /*IsModuleFile=*/false,
90
9.12k
                                    /*IsMissing=*/false);
91
9.12k
  }
92
93
3.24k
  void EndOfMainFile() override { DepCollector.finishedMainFile(Diags); }
94
};
95
96
struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
97
  DependencyCollector &DepCollector;
98
3.24k
  DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
99
100
  void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
101
5.24k
                         bool IsSystem) override {
102
5.24k
    StringRef Filename = Entry.getName();
103
5.24k
    DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
104
5.24k
                                    /*IsSystem*/IsSystem,
105
5.24k
                                    /*IsModuleFile*/false,
106
5.24k
                                    /*IsMissing*/false);
107
5.24k
  }
108
};
109
110
struct DepCollectorASTListener : public ASTReaderListener {
111
  DependencyCollector &DepCollector;
112
868
  DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
113
460k
  bool needsInputFileVisitation() override { return true; }
114
457k
  bool needsSystemInputFileVisitation() override {
115
457k
    return DepCollector.needSystemDependencies();
116
457k
  }
117
  void visitModuleFile(StringRef Filename,
118
4.48k
                       serialization::ModuleKind Kind) override {
119
4.48k
    DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
120
4.48k
                                   /*IsSystem*/false, /*IsModuleFile*/true,
121
4.48k
                                   /*IsMissing*/false);
122
4.48k
  }
123
  bool visitInputFile(StringRef Filename, bool IsSystem,
124
455k
                      bool IsOverridden, bool IsExplicitModule) override {
125
455k
    if (IsOverridden || 
IsExplicitModule455k
)
126
165
      return true;
127
128
455k
    DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
129
455k
                                   /*IsModuleFile*/false, /*IsMissing*/false);
130
455k
    return true;
131
455k
  }
132
};
133
} // end anonymous namespace
134
135
void DependencyCollector::maybeAddDependency(StringRef Filename,
136
                                             bool FromModule, bool IsSystem,
137
                                             bool IsModuleFile,
138
1.58M
                                             bool IsMissing) {
139
1.58M
  if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
140
1.57M
    addDependency(Filename);
141
1.58M
}
142
143
1.57M
bool DependencyCollector::addDependency(StringRef Filename) {
144
1.57M
  if (Seen.insert(Filename).second) {
145
780k
    Dependencies.push_back(std::string(Filename));
146
780k
    return true;
147
780k
  }
148
797k
  return false;
149
797k
}
150
151
1.57M
static bool isSpecialFilename(StringRef Filename) {
152
1.57M
  return llvm::StringSwitch<bool>(Filename)
153
1.57M
      .Case("<built-in>", true)
154
1.57M
      .Case("<stdin>", true)
155
1.57M
      .Default(false);
156
1.57M
}
157
158
bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
159
                                        bool IsSystem, bool IsModuleFile,
160
270
                                        bool IsMissing) {
161
270
  return !isSpecialFilename(Filename) &&
162
270
         (needSystemDependencies() || 
!IsSystem0
);
163
270
}
164
165
288
DependencyCollector::~DependencyCollector() { }
166
3.24k
void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
167
3.24k
  PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(
168
3.24k
      *this, PP.getSourceManager(), PP.getDiagnostics()));
169
3.24k
  PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
170
3.24k
      std::make_unique<DepCollectorMMCallbacks>(*this));
171
3.24k
}
172
867
void DependencyCollector::attachToASTReader(ASTReader &R) {
173
867
  R.addListener(std::make_unique<DepCollectorASTListener>(*this));
174
867
}
175
176
DependencyFileGenerator::DependencyFileGenerator(
177
    const DependencyOutputOptions &Opts)
178
    : OutputFile(Opts.OutputFile), Targets(Opts.Targets),
179
      IncludeSystemHeaders(Opts.IncludeSystemHeaders),
180
      PhonyTarget(Opts.UsePhonyTargets),
181
      AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),
182
      IncludeModuleFiles(Opts.IncludeModuleFiles),
183
3.21k
      OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
184
14
  for (const auto &ExtraDep : Opts.ExtraDeps) {
185
14
    if (addDependency(ExtraDep))
186
13
      ++InputFileIndex;
187
14
  }
188
3.21k
}
189
190
3.15k
void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
191
  // Disable the "file not found" diagnostic if the -MG option was given.
192
3.15k
  if (AddMissingHeaderDeps)
193
4
    PP.SetSuppressIncludeNotFoundError(true);
194
195
3.15k
  DependencyCollector::attachToPreprocessor(PP);
196
3.15k
}
197
198
bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
199
                                            bool IsSystem, bool IsModuleFile,
200
1.58M
                                            bool IsMissing) {
201
1.58M
  if (IsMissing) {
202
    // Handle the case of missing file from an inclusion directive.
203
20
    if (AddMissingHeaderDeps)
204
17
      return true;
205
3
    SeenMissingHeader = true;
206
3
    return false;
207
3
  }
208
1.58M
  if (IsModuleFile && 
!IncludeModuleFiles4.46k
)
209
4.46k
    return false;
210
211
1.57M
  if (isSpecialFilename(Filename))
212
0
    return false;
213
214
1.57M
  if (IncludeSystemHeaders)
215
1.57M
    return true;
216
217
96
  return !IsSystem;
218
96
}
219
220
3.07k
void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
221
3.07k
  outputDependencyFile(Diags);
222
3.07k
}
223
224
/// Print the filename, with escaping or quoting that accommodates the three
225
/// most likely tools that use dependency files: GNU Make, BSD Make, and
226
/// NMake/Jom.
227
///
228
/// BSD Make is the simplest case: It does no escaping at all.  This means
229
/// characters that are normally delimiters, i.e. space and # (the comment
230
/// character) simply aren't supported in filenames.
231
///
232
/// GNU Make does allow space and # in filenames, but to avoid being treated
233
/// as a delimiter or comment, these must be escaped with a backslash. Because
234
/// backslash is itself the escape character, if a backslash appears in a
235
/// filename, it should be escaped as well.  (As a special case, $ is escaped
236
/// as $$, which is the normal Make way to handle the $ character.)
237
/// For compatibility with BSD Make and historical practice, if GNU Make
238
/// un-escapes characters in a filename but doesn't find a match, it will
239
/// retry with the unmodified original string.
240
///
241
/// GCC tries to accommodate both Make formats by escaping any space or #
242
/// characters in the original filename, but not escaping backslashes.  The
243
/// apparent intent is so that filenames with backslashes will be handled
244
/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
245
/// unmodified original string; filenames with # or space characters aren't
246
/// supported by BSD Make at all, but will be handled correctly by GNU Make
247
/// due to the escaping.
248
///
249
/// A corner case that GCC gets only partly right is when the original filename
250
/// has a backslash immediately followed by space or #.  GNU Make would expect
251
/// this backslash to be escaped; however GCC escapes the original backslash
252
/// only when followed by space, not #.  It will therefore take a dependency
253
/// from a directive such as
254
///     #include "a\ b\#c.h"
255
/// and emit it as
256
///     a\\\ b\\#c.h
257
/// which GNU Make will interpret as
258
///     a\ b\
259
/// followed by a comment. Failing to find this file, it will fall back to the
260
/// original string, which probably doesn't exist either; in any case it won't
261
/// find
262
///     a\ b\#c.h
263
/// which is the actual filename specified by the include directive.
264
///
265
/// Clang does what GCC does, rather than what GNU Make expects.
266
///
267
/// NMake/Jom has a different set of scary characters, but wraps filespecs in
268
/// double-quotes to avoid misinterpreting them; see
269
/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
270
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
271
/// for Windows file-naming info.
272
static void PrintFilename(raw_ostream &OS, StringRef Filename,
273
1.55M
                          DependencyOutputFormat OutputFormat) {
274
  // Convert filename to platform native path
275
1.55M
  llvm::SmallString<256> NativePath;
276
1.55M
  llvm::sys::path::native(Filename.str(), NativePath);
277
278
1.55M
  if (OutputFormat == DependencyOutputFormat::NMake) {
279
    // Add quotes if needed. These are the characters listed as "special" to
280
    // NMake, that are legal in a Windows filespec, and that could cause
281
    // misinterpretation of the dependency string.
282
6
    if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
283
4
      OS << '\"' << NativePath << '\"';
284
2
    else
285
2
      OS << NativePath;
286
6
    return;
287
6
  }
288
1.55M
  assert(OutputFormat == DependencyOutputFormat::Make);
289
218M
  for (unsigned i = 0, e = NativePath.size(); i != e; 
++i217M
) {
290
217M
    if (NativePath[i] == '#') // Handle '#' the broken gcc way.
291
6
      OS << '\\';
292
217M
    else if (NativePath[i] == ' ') { // Handle space correctly.
293
10
      OS << '\\';
294
10
      unsigned j = i;
295
10
      while (j > 0 && 
NativePath[--j] == '\\'8
)
296
0
        OS << '\\';
297
217M
    } else if (NativePath[i] == '$') // $ is escaped by $$.
298
4
      OS << '$';
299
217M
    OS << NativePath[i];
300
217M
  }
301
1.55M
}
302
303
3.07k
void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
304
3.07k
  if (SeenMissingHeader) {
305
1
    llvm::sys::fs::remove(OutputFile);
306
1
    return;
307
1
  }
308
309
3.07k
  std::error_code EC;
310
3.07k
  llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
311
3.07k
  if (EC) {
312
0
    Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();
313
0
    return;
314
0
  }
315
316
3.07k
  outputDependencyFile(OS);
317
3.07k
}
318
319
3.14k
void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
320
  // Write out the dependency targets, trying to avoid overly long
321
  // lines when possible. We try our best to emit exactly the same
322
  // dependency file as GCC (4.2), assuming the included files are the
323
  // same.
324
3.14k
  const unsigned MaxColumns = 75;
325
3.14k
  unsigned Columns = 0;
326
327
3.14k
  for (StringRef Target : Targets) {
328
3.14k
    unsigned N = Target.size();
329
3.14k
    if (Columns == 0) {
330
3.14k
      Columns += N;
331
6
    } else if (Columns + N + 2 > MaxColumns) {
332
0
      Columns = N + 2;
333
0
      OS << " \\\n  ";
334
6
    } else {
335
6
      Columns += N + 1;
336
6
      OS << ' ';
337
6
    }
338
    // Targets already quoted as needed.
339
3.14k
    OS << Target;
340
3.14k
  }
341
342
3.14k
  OS << ':';
343
3.14k
  Columns += 1;
344
345
  // Now add each dependency in the order it was seen, but avoiding
346
  // duplicates.
347
3.14k
  ArrayRef<std::string> Files = getDependencies();
348
779k
  for (StringRef File : Files) {
349
    // Start a new line if this would exceed the column limit. Make
350
    // sure to leave space for a trailing " \" in case we need to
351
    // break the line on the next iteration.
352
779k
    unsigned N = File.size();
353
779k
    if (Columns + (N + 1) + 2 > MaxColumns) {
354
779k
      OS << " \\\n ";
355
779k
      Columns = 2;
356
779k
    }
357
779k
    OS << ' ';
358
779k
    PrintFilename(OS, File, OutputFormat);
359
779k
    Columns += N + 1;
360
779k
  }
361
3.14k
  OS << '\n';
362
363
  // Create phony targets if requested.
364
3.14k
  if (PhonyTarget && 
!Files.empty()3.02k
) {
365
3.02k
    unsigned Index = 0;
366
782k
    for (auto I = Files.begin(), E = Files.end(); I != E; 
++I779k
) {
367
779k
      if (Index++ == InputFileIndex)
368
3.02k
        continue;
369
776k
      OS << '\n';
370
776k
      PrintFilename(OS, *I, OutputFormat);
371
776k
      OS << ":\n";
372
776k
    }
373
3.02k
  }
374
3.14k
}