2011-02-20 13:06:31 +00:00
|
|
|
//===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-04-14 14:01:31 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-02-20 13:06:31 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class HeaderIncludesCallback : public PPCallbacks {
|
|
|
|
SourceManager &SM;
|
2011-10-20 21:14:49 +00:00
|
|
|
raw_ostream *OutputFile;
|
2011-02-20 13:06:31 +00:00
|
|
|
unsigned CurrentIncludeDepth;
|
|
|
|
bool HasProcessedPredefines;
|
|
|
|
bool OwnsOutputFile;
|
|
|
|
bool ShowAllHeaders;
|
2011-05-02 19:39:53 +00:00
|
|
|
bool ShowDepth;
|
2013-12-22 00:07:40 +00:00
|
|
|
bool MSStyle;
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
|
2011-10-20 21:14:49 +00:00
|
|
|
raw_ostream *OutputFile_, bool OwnsOutputFile_,
|
2013-12-22 00:07:40 +00:00
|
|
|
bool ShowDepth_, bool MSStyle_)
|
2011-02-20 13:06:31 +00:00
|
|
|
: SM(PP->getSourceManager()), OutputFile(OutputFile_),
|
|
|
|
CurrentIncludeDepth(0), HasProcessedPredefines(false),
|
2011-05-02 19:39:53 +00:00
|
|
|
OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
|
2013-12-22 00:07:40 +00:00
|
|
|
ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
~HeaderIncludesCallback() {
|
|
|
|
if (OwnsOutputFile)
|
|
|
|
delete OutputFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2011-10-20 21:14:49 +00:00
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID);
|
2011-02-20 13:06:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
|
2013-12-22 00:07:40 +00:00
|
|
|
StringRef OutputPath, bool ShowDepth,
|
|
|
|
bool MSStyle) {
|
2011-10-20 21:14:49 +00:00
|
|
|
raw_ostream *OutputFile = &llvm::errs();
|
2011-02-20 13:06:31 +00:00
|
|
|
bool OwnsOutputFile = false;
|
|
|
|
|
|
|
|
// Open the output file, if used.
|
|
|
|
if (!OutputPath.empty()) {
|
|
|
|
std::string Error;
|
|
|
|
llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
|
2013-12-22 00:07:40 +00:00
|
|
|
OutputPath.str().c_str(), Error, llvm::sys::fs::F_Append);
|
2011-02-20 13:06:31 +00:00
|
|
|
if (!Error.empty()) {
|
|
|
|
PP.getDiagnostics().Report(
|
|
|
|
clang::diag::warn_fe_cc_print_header_failure) << Error;
|
|
|
|
delete OS;
|
|
|
|
} else {
|
|
|
|
OS->SetUnbuffered();
|
|
|
|
OS->SetUseAtomicWrites(true);
|
|
|
|
OutputFile = OS;
|
|
|
|
OwnsOutputFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
|
2011-05-02 19:39:53 +00:00
|
|
|
OutputFile, OwnsOutputFile,
|
2013-12-22 00:07:40 +00:00
|
|
|
ShowDepth, MSStyle));
|
2011-02-20 13:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
2011-10-20 21:14:49 +00:00
|
|
|
SrcMgr::CharacteristicKind NewFileType,
|
|
|
|
FileID PrevFID) {
|
2011-02-20 13:06:31 +00: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;
|
|
|
|
|
|
|
|
// Adjust the current include depth.
|
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
|
|
|
++CurrentIncludeDepth;
|
2011-05-02 19:39:53 +00:00
|
|
|
} else if (Reason == PPCallbacks::ExitFile) {
|
2011-02-20 13:06:31 +00:00
|
|
|
if (CurrentIncludeDepth)
|
|
|
|
--CurrentIncludeDepth;
|
|
|
|
|
|
|
|
// We track when we are done with the predefines by watching for the first
|
2011-05-02 19:39:53 +00:00
|
|
|
// place where we drop back to a nesting depth of 1.
|
|
|
|
if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
|
2011-02-20 13:06:31 +00:00
|
|
|
HasProcessedPredefines = true;
|
2011-05-02 19:39:53 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
2011-02-20 13:06:31 +00: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));
|
|
|
|
|
|
|
|
// Dump the header include information we are past the predefines buffer or
|
|
|
|
// are showing all headers.
|
|
|
|
if (ShowHeader && Reason == PPCallbacks::EnterFile) {
|
|
|
|
// Write to a temporary string to avoid unnecessary flushing on errs().
|
2012-04-14 14:01:31 +00:00
|
|
|
SmallString<512> Filename(UserLoc.getFilename());
|
2013-12-22 00:07:40 +00:00
|
|
|
if (!MSStyle)
|
|
|
|
Lexer::Stringify(Filename);
|
2011-02-20 13:06:31 +00:00
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
SmallString<256> Msg;
|
2013-12-22 00:07:40 +00:00
|
|
|
if (MSStyle)
|
|
|
|
Msg += "Note: including file:";
|
|
|
|
|
2011-05-02 19:39:53 +00:00
|
|
|
if (ShowDepth) {
|
|
|
|
// The main source file is at depth 1, so skip one dot.
|
|
|
|
for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
|
2013-12-22 00:07:40 +00:00
|
|
|
Msg += MSStyle ? ' ' : '.';
|
|
|
|
|
|
|
|
if (!MSStyle)
|
|
|
|
Msg += ' ';
|
2011-05-02 19:39:53 +00:00
|
|
|
}
|
2011-02-20 13:06:31 +00:00
|
|
|
Msg += Filename;
|
|
|
|
Msg += '\n';
|
|
|
|
|
|
|
|
OutputFile->write(Msg.data(), Msg.size());
|
|
|
|
}
|
|
|
|
}
|