blob: 79920df20af75746106ec0752153d4e4c8cc27a6 [file] [log] [blame]
Daniel Dunbareef63e02011-02-02 15:41:17 +00001//===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Frontend/Utils.h"
11#include "clang/Basic/SourceManager.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000012#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000013#include "clang/Lex/Preprocessor.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000016using namespace clang;
17
18namespace {
19class HeaderIncludesCallback : public PPCallbacks {
20 SourceManager &SM;
Chris Lattner5f9e2722011-07-23 10:55:15 +000021 raw_ostream *OutputFile;
Daniel Dunbareef63e02011-02-02 15:41:17 +000022 unsigned CurrentIncludeDepth;
23 bool HasProcessedPredefines;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000024 bool OwnsOutputFile;
25 bool ShowAllHeaders;
Daniel Dunbarda608852011-03-21 19:37:38 +000026 bool ShowDepth;
Daniel Dunbareef63e02011-02-02 15:41:17 +000027
28public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000029 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Chris Lattner5f9e2722011-07-23 10:55:15 +000030 raw_ostream *OutputFile_, bool OwnsOutputFile_,
Daniel Dunbarda608852011-03-21 19:37:38 +000031 bool ShowDepth_)
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000032 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
33 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarda608852011-03-21 19:37:38 +000034 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
35 ShowDepth(ShowDepth_) {}
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000036
37 ~HeaderIncludesCallback() {
38 if (OwnsOutputFile)
Daniel Dunbarf704c612011-02-03 03:45:00 +000039 delete OutputFile;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000040 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000041
42 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000043 SrcMgr::CharacteristicKind FileType,
44 FileID PrevFID);
Daniel Dunbareef63e02011-02-02 15:41:17 +000045};
46}
47
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000048void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
Chris Lattner5f9e2722011-07-23 10:55:15 +000049 StringRef OutputPath, bool ShowDepth) {
50 raw_ostream *OutputFile = &llvm::errs();
Daniel Dunbarf704c612011-02-03 03:45:00 +000051 bool OwnsOutputFile = false;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000052
53 // Open the output file, if used.
Daniel Dunbarf704c612011-02-03 03:45:00 +000054 if (!OutputPath.empty()) {
55 std::string Error;
56 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
57 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
58 if (!Error.empty()) {
59 PP.getDiagnostics().Report(
60 clang::diag::warn_fe_cc_print_header_failure) << Error;
61 delete OS;
62 } else {
63 OS->SetUnbuffered();
64 OS->SetUseAtomicWrites(true);
65 OutputFile = OS;
66 OwnsOutputFile = true;
67 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000068 }
69
70 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
Daniel Dunbarda608852011-03-21 19:37:38 +000071 OutputFile, OwnsOutputFile,
72 ShowDepth));
Daniel Dunbareef63e02011-02-02 15:41:17 +000073}
74
75void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
76 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000077 SrcMgr::CharacteristicKind NewFileType,
78 FileID PrevFID) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000079 // Unless we are exiting a #include, make sure to skip ahead to the line the
80 // #include directive was at.
81 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
82 if (UserLoc.isInvalid())
83 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000084
Daniel Dunbareef63e02011-02-02 15:41:17 +000085 // Adjust the current include depth.
86 if (Reason == PPCallbacks::EnterFile) {
87 ++CurrentIncludeDepth;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000088 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000089 if (CurrentIncludeDepth)
90 --CurrentIncludeDepth;
91
92 // We track when we are done with the predefines by watching for the first
Sebastian Redl4dddebf2011-04-14 14:07:45 +000093 // place where we drop back to a nesting depth of 1.
94 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbareef63e02011-02-02 15:41:17 +000095 HasProcessedPredefines = true;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000096
97 return;
98 } else
99 return;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +0000100
101 // Show the header if we are (a) past the predefines, or (b) showing all
102 // headers and in the predefines at a depth past the initial file and command
103 // line buffers.
104 bool ShowHeader = (HasProcessedPredefines ||
105 (ShowAllHeaders && CurrentIncludeDepth > 2));
106
107 // Dump the header include information we are past the predefines buffer or
108 // are showing all headers.
109 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000110 // Write to a temporary string to avoid unnecessary flushing on errs().
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000111 SmallString<512> Filename(UserLoc.getFilename());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000112 Lexer::Stringify(Filename);
113
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000114 SmallString<256> Msg;
Daniel Dunbarda608852011-03-21 19:37:38 +0000115 if (ShowDepth) {
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000116 // The main source file is at depth 1, so skip one dot.
117 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Daniel Dunbarda608852011-03-21 19:37:38 +0000118 Msg += '.';
119 Msg += ' ';
120 }
Daniel Dunbareef63e02011-02-02 15:41:17 +0000121 Msg += Filename;
122 Msg += '\n';
123
Daniel Dunbarf704c612011-02-03 03:45:00 +0000124 OutputFile->write(Msg.data(), Msg.size());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000125 }
126}