2019-03-01 14:49:51 +08:00
|
|
|
//===-- HeaderIncludeGen.cpp - Generate Header Includes -------------------===//
|
2011-02-02 23:41:17 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-02-02 23:41:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-03-24 02:46:57 +08:00
|
|
|
#include "clang/Frontend/DependencyOutputOptions.h"
|
2011-02-02 23:41:17 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2011-02-03 11:45:00 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2011-02-02 23:41:17 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-02-03 11:45:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-02-02 23:41:17 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class HeaderIncludesCallback : public PPCallbacks {
|
|
|
|
SourceManager &SM;
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OutputFile;
|
2016-03-24 02:46:57 +08:00
|
|
|
const DependencyOutputOptions &DepOpts;
|
2011-02-02 23:41:17 +08:00
|
|
|
unsigned CurrentIncludeDepth;
|
|
|
|
bool HasProcessedPredefines;
|
2011-02-03 05:11:31 +08:00
|
|
|
bool OwnsOutputFile;
|
|
|
|
bool ShowAllHeaders;
|
2011-03-22 03:37:38 +08:00
|
|
|
bool ShowDepth;
|
2013-08-09 08:32:23 +08:00
|
|
|
bool MSStyle;
|
2011-02-02 23:41:17 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-03 05:11:31 +08:00
|
|
|
HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
|
2016-03-13 10:44:13 +08:00
|
|
|
raw_ostream *OutputFile_,
|
2016-03-24 02:46:57 +08:00
|
|
|
const DependencyOutputOptions &DepOpts,
|
2016-03-13 10:44:13 +08:00
|
|
|
bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
|
2016-03-24 02:46:57 +08:00
|
|
|
: SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
|
|
|
|
CurrentIncludeDepth(0), HasProcessedPredefines(false),
|
|
|
|
OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
|
|
|
|
ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
|
2011-02-03 05:11:31 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
~HeaderIncludesCallback() override {
|
2011-02-03 05:11:31 +08:00
|
|
|
if (OwnsOutputFile)
|
2011-02-03 11:45:00 +08:00
|
|
|
delete OutputFile;
|
2011-02-03 05:11:31 +08:00
|
|
|
}
|
2011-02-02 23:41:17 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID) override;
|
2021-04-13 19:59:03 +08:00
|
|
|
|
|
|
|
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override;
|
2011-02-02 23:41:17 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-02 23:41:17 +08:00
|
|
|
|
2016-05-27 20:52:19 +08:00
|
|
|
static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
|
2015-08-13 12:04:37 +08:00
|
|
|
bool ShowDepth, unsigned CurrentIncludeDepth,
|
|
|
|
bool MSStyle) {
|
2016-05-27 20:52:19 +08:00
|
|
|
// Write to a temporary string to avoid unnecessary flushing on errs().
|
|
|
|
SmallString<512> Pathname(Filename);
|
|
|
|
if (!MSStyle)
|
|
|
|
Lexer::Stringify(Pathname);
|
2015-08-13 12:04:37 +08:00
|
|
|
|
2016-05-27 20:52:19 +08:00
|
|
|
SmallString<256> Msg;
|
|
|
|
if (MSStyle)
|
|
|
|
Msg += "Note: including file:";
|
2015-08-13 12:04:37 +08:00
|
|
|
|
2016-05-27 20:52:19 +08:00
|
|
|
if (ShowDepth) {
|
|
|
|
// The main source file is at depth 1, so skip one dot.
|
|
|
|
for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
|
|
|
|
Msg += MSStyle ? ' ' : '.';
|
2015-08-13 12:04:37 +08:00
|
|
|
|
2016-05-27 20:52:19 +08:00
|
|
|
if (!MSStyle)
|
|
|
|
Msg += ' ';
|
|
|
|
}
|
|
|
|
Msg += Pathname;
|
|
|
|
Msg += '\n';
|
2015-08-13 12:04:37 +08:00
|
|
|
|
2016-05-27 20:52:19 +08:00
|
|
|
*OutputFile << Msg;
|
|
|
|
OutputFile->flush();
|
2015-08-13 12:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang::AttachHeaderIncludeGen(Preprocessor &PP,
|
2016-03-24 02:46:57 +08:00
|
|
|
const DependencyOutputOptions &DepOpts,
|
|
|
|
bool ShowAllHeaders, StringRef OutputPath,
|
|
|
|
bool ShowDepth, bool MSStyle) {
|
2018-05-04 23:58:31 +08:00
|
|
|
raw_ostream *OutputFile = &llvm::errs();
|
2011-02-03 11:45:00 +08:00
|
|
|
bool OwnsOutputFile = false;
|
2011-02-03 05:11:31 +08:00
|
|
|
|
2018-05-04 23:58:31 +08:00
|
|
|
// Choose output stream, when printing in cl.exe /showIncludes style.
|
|
|
|
if (MSStyle) {
|
|
|
|
switch (DepOpts.ShowIncludesDest) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid destination for /showIncludes output!");
|
|
|
|
case ShowIncludesDestination::Stderr:
|
|
|
|
OutputFile = &llvm::errs();
|
|
|
|
break;
|
|
|
|
case ShowIncludesDestination::Stdout:
|
|
|
|
OutputFile = &llvm::outs();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 05:11:31 +08:00
|
|
|
// Open the output file, if used.
|
2011-02-03 11:45:00 +08:00
|
|
|
if (!OutputPath.empty()) {
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
2011-02-03 11:45:00 +08:00
|
|
|
llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
|
2019-08-05 13:43:48 +08:00
|
|
|
OutputPath.str(), EC,
|
2021-04-06 19:22:41 +08:00
|
|
|
llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
|
2014-08-26 02:17:04 +08:00
|
|
|
if (EC) {
|
|
|
|
PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
|
|
|
|
<< EC.message();
|
2011-02-03 11:45:00 +08:00
|
|
|
delete OS;
|
|
|
|
} else {
|
|
|
|
OS->SetUnbuffered();
|
|
|
|
OutputFile = OS;
|
|
|
|
OwnsOutputFile = true;
|
|
|
|
}
|
2011-02-03 05:11:31 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:46:57 +08:00
|
|
|
// Print header info for extra headers, pretending they were discovered by
|
|
|
|
// the regular preprocessor. The primary use case is to support proper
|
|
|
|
// generation of Make / Ninja file dependencies for implicit includes, such
|
2021-11-12 22:30:06 +08:00
|
|
|
// as sanitizer ignorelists. It's only important for cl.exe compatibility,
|
2016-03-24 02:46:57 +08:00
|
|
|
// the GNU way to generate rules is -M / -MM / -MD / -MMD.
|
2016-05-27 20:52:19 +08:00
|
|
|
for (const auto &Header : DepOpts.ExtraDeps)
|
2021-02-10 18:47:33 +08:00
|
|
|
PrintHeaderInfo(OutputFile, Header.first, ShowDepth, 2, MSStyle);
|
2019-08-15 07:04:18 +08:00
|
|
|
PP.addPPCallbacks(std::make_unique<HeaderIncludesCallback>(
|
2016-03-24 02:46:57 +08:00
|
|
|
&PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
|
2016-03-13 10:44:13 +08:00
|
|
|
MSStyle));
|
2011-02-02 23:41:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
2020-02-25 05:07:16 +08:00
|
|
|
SrcMgr::CharacteristicKind NewFileType,
|
|
|
|
FileID PrevFID) {
|
2011-02-02 23:41:17 +08:00
|
|
|
// Unless we are exiting a #include, make sure to skip ahead to the line the
|
|
|
|
// #include directive was at.
|
|
|
|
PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
|
|
|
|
if (UserLoc.isInvalid())
|
|
|
|
return;
|
2011-02-03 05:11:31 +08:00
|
|
|
|
2011-02-02 23:41:17 +08:00
|
|
|
// Adjust the current include depth.
|
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
|
|
|
++CurrentIncludeDepth;
|
2011-04-14 22:07:45 +08:00
|
|
|
} else if (Reason == PPCallbacks::ExitFile) {
|
2011-02-02 23:41:17 +08:00
|
|
|
if (CurrentIncludeDepth)
|
|
|
|
--CurrentIncludeDepth;
|
|
|
|
|
|
|
|
// We track when we are done with the predefines by watching for the first
|
2011-04-14 22:07:45 +08:00
|
|
|
// place where we drop back to a nesting depth of 1.
|
2016-03-24 02:46:57 +08:00
|
|
|
if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) {
|
|
|
|
if (!DepOpts.ShowIncludesPretendHeader.empty()) {
|
2016-05-27 20:52:19 +08:00
|
|
|
PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader,
|
2016-03-24 02:46:57 +08:00
|
|
|
ShowDepth, 2, MSStyle);
|
|
|
|
}
|
2011-02-02 23:41:17 +08:00
|
|
|
HasProcessedPredefines = true;
|
2016-03-24 02:46:57 +08:00
|
|
|
}
|
2011-04-14 22:07:45 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
2011-02-03 05:11:24 +08:00
|
|
|
|
|
|
|
// Show the header if we are (a) past the predefines, or (b) showing all
|
|
|
|
// headers and in the predefines at a depth past the initial file and command
|
|
|
|
// line buffers.
|
|
|
|
bool ShowHeader = (HasProcessedPredefines ||
|
|
|
|
(ShowAllHeaders && CurrentIncludeDepth > 2));
|
2016-03-24 02:00:22 +08:00
|
|
|
unsigned IncludeDepth = CurrentIncludeDepth;
|
|
|
|
if (!HasProcessedPredefines)
|
2016-03-24 02:46:57 +08:00
|
|
|
--IncludeDepth; // Ignore indent from <built-in>.
|
|
|
|
else if (!DepOpts.ShowIncludesPretendHeader.empty())
|
|
|
|
++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
|
2011-02-03 05:11:24 +08:00
|
|
|
|
2020-02-25 05:07:16 +08:00
|
|
|
if (!DepOpts.IncludeSystemHeaders && isSystem(NewFileType))
|
|
|
|
ShowHeader = false;
|
|
|
|
|
2011-02-03 05:11:24 +08:00
|
|
|
// Dump the header include information we are past the predefines buffer or
|
2016-03-24 02:00:22 +08:00
|
|
|
// are showing all headers and this isn't the magic implicit <command line>
|
|
|
|
// header.
|
|
|
|
// FIXME: Identify headers in a more robust way than comparing their name to
|
|
|
|
// "<command line>" and "<built-in>" in a bunch of places.
|
|
|
|
if (ShowHeader && Reason == PPCallbacks::EnterFile &&
|
|
|
|
UserLoc.getFilename() != StringRef("<command line>")) {
|
|
|
|
PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
|
|
|
|
MSStyle);
|
2011-02-02 23:41:17 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 19:59:03 +08:00
|
|
|
|
|
|
|
void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
|
|
|
|
Token &FilenameTok,
|
|
|
|
SrcMgr::CharacteristicKind FileType) {
|
|
|
|
if (!DepOpts.ShowSkippedHeaderIncludes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!DepOpts.IncludeSystemHeaders && isSystem(FileType))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PrintHeaderInfo(OutputFile, SkippedFile.getName(), ShowDepth,
|
|
|
|
CurrentIncludeDepth + 1, MSStyle);
|
|
|
|
}
|