2011-02-02 23:41:17 +08: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"
|
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;
|
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;
|
2011-02-02 23:41:17 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-03 05:11:31 +08:00
|
|
|
HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OutputFile_, bool OwnsOutputFile_,
|
2011-03-22 03:37:38 +08:00
|
|
|
bool ShowDepth_)
|
2011-02-03 05:11:31 +08:00
|
|
|
: SM(PP->getSourceManager()), OutputFile(OutputFile_),
|
|
|
|
CurrentIncludeDepth(0), HasProcessedPredefines(false),
|
2011-03-22 03:37:38 +08:00
|
|
|
OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
|
|
|
|
ShowDepth(ShowDepth_) {}
|
2011-02-03 05:11:31 +08:00
|
|
|
|
|
|
|
~HeaderIncludesCallback() {
|
|
|
|
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
|
|
|
|
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2011-10-12 01:29:44 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID);
|
2011-02-02 23:41:17 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-03 05:11:24 +08:00
|
|
|
void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef OutputPath, bool ShowDepth) {
|
|
|
|
raw_ostream *OutputFile = &llvm::errs();
|
2011-02-03 11:45:00 +08:00
|
|
|
bool OwnsOutputFile = false;
|
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()) {
|
|
|
|
std::string Error;
|
|
|
|
llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
|
|
|
|
OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
|
|
|
|
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;
|
|
|
|
}
|
2011-02-03 05:11:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
|
2011-03-22 03:37:38 +08:00
|
|
|
OutputFile, OwnsOutputFile,
|
|
|
|
ShowDepth));
|
2011-02-02 23:41:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
2011-10-12 01:29:44 +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.
|
|
|
|
if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
|
2011-02-02 23:41:17 +08:00
|
|
|
HasProcessedPredefines = true;
|
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));
|
|
|
|
|
|
|
|
// Dump the header include information we are past the predefines buffer or
|
|
|
|
// are showing all headers.
|
|
|
|
if (ShowHeader && Reason == PPCallbacks::EnterFile) {
|
2011-02-02 23:41:17 +08:00
|
|
|
// Write to a temporary string to avoid unnecessary flushing on errs().
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<512> Filename(UserLoc.getFilename());
|
2011-02-02 23:41:17 +08:00
|
|
|
Lexer::Stringify(Filename);
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Msg;
|
2011-03-22 03:37:38 +08:00
|
|
|
if (ShowDepth) {
|
2011-04-14 22:07:45 +08:00
|
|
|
// The main source file is at depth 1, so skip one dot.
|
|
|
|
for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
|
2011-03-22 03:37:38 +08:00
|
|
|
Msg += '.';
|
|
|
|
Msg += ' ';
|
|
|
|
}
|
2011-02-02 23:41:17 +08:00
|
|
|
Msg += Filename;
|
|
|
|
Msg += '\n';
|
|
|
|
|
2011-02-03 11:45:00 +08:00
|
|
|
OutputFile->write(Msg.data(), Msg.size());
|
2011-02-02 23:41:17 +08:00
|
|
|
}
|
|
|
|
}
|