blob: 5732e5b3fb73049869855e367028d6a54a0f8976 [file] [log] [blame]
Daniel Dunbar27734fd2011-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 Dunbar9aa47fc2011-02-03 03:45:00 +000012#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000013#include "clang/Lex/Preprocessor.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000016using namespace clang;
17
18namespace {
19class HeaderIncludesCallback : public PPCallbacks {
20 SourceManager &SM;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000021 raw_ostream *OutputFile;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000022 unsigned CurrentIncludeDepth;
23 bool HasProcessedPredefines;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000024 bool OwnsOutputFile;
25 bool ShowAllHeaders;
Daniel Dunbarfe908a82011-03-21 19:37:38 +000026 bool ShowDepth;
Hans Wennborg0fd62072013-08-09 00:32:23 +000027 bool MSStyle;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000028
29public:
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000030 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031 raw_ostream *OutputFile_, bool OwnsOutputFile_,
Hans Wennborg0fd62072013-08-09 00:32:23 +000032 bool ShowDepth_, bool MSStyle_)
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000033 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarfe908a82011-03-21 19:37:38 +000035 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
Hans Wennborg0fd62072013-08-09 00:32:23 +000036 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000037
Alexander Kornienko34eb2072015-04-11 02:00:23 +000038 ~HeaderIncludesCallback() override {
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000039 if (OwnsOutputFile)
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000040 delete OutputFile;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000041 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +000042
Craig Topperafa7cb32014-03-13 06:07:04 +000043 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44 SrcMgr::CharacteristicKind FileType,
45 FileID PrevFID) override;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000046};
47}
48
Daniel Dunbarfb244852011-02-02 21:11:24 +000049void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
Hans Wennborg0fd62072013-08-09 00:32:23 +000050 StringRef OutputPath, bool ShowDepth,
51 bool MSStyle) {
Nico Weber25828952014-07-06 03:04:24 +000052 raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000053 bool OwnsOutputFile = false;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000054
55 // Open the output file, if used.
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000056 if (!OutputPath.empty()) {
Rafael Espindoladae941a2014-08-25 18:17:04 +000057 std::error_code EC;
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000058 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Rafael Espindoladae941a2014-08-25 18:17:04 +000059 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
60 if (EC) {
61 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
62 << EC.message();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000063 delete OS;
64 } else {
65 OS->SetUnbuffered();
66 OS->SetUseAtomicWrites(true);
67 OutputFile = OS;
68 OwnsOutputFile = true;
69 }
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000070 }
71
Craig Topperb8a70532014-09-10 04:53:53 +000072 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
73 ShowAllHeaders,
74 OutputFile,
75 OwnsOutputFile,
76 ShowDepth,
77 MSStyle));
Daniel Dunbar27734fd2011-02-02 15:41:17 +000078}
79
80void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
81 FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +000082 SrcMgr::CharacteristicKind NewFileType,
83 FileID PrevFID) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +000084 // Unless we are exiting a #include, make sure to skip ahead to the line the
85 // #include directive was at.
86 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
87 if (UserLoc.isInvalid())
88 return;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000089
Daniel Dunbar27734fd2011-02-02 15:41:17 +000090 // Adjust the current include depth.
91 if (Reason == PPCallbacks::EnterFile) {
92 ++CurrentIncludeDepth;
Sebastian Redl4e521232011-04-14 14:07:45 +000093 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +000094 if (CurrentIncludeDepth)
95 --CurrentIncludeDepth;
96
97 // We track when we are done with the predefines by watching for the first
Sebastian Redl4e521232011-04-14 14:07:45 +000098 // place where we drop back to a nesting depth of 1.
99 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000100 HasProcessedPredefines = true;
Sebastian Redl4e521232011-04-14 14:07:45 +0000101
102 return;
103 } else
104 return;
Daniel Dunbarfb244852011-02-02 21:11:24 +0000105
106 // Show the header if we are (a) past the predefines, or (b) showing all
107 // headers and in the predefines at a depth past the initial file and command
108 // line buffers.
109 bool ShowHeader = (HasProcessedPredefines ||
110 (ShowAllHeaders && CurrentIncludeDepth > 2));
111
112 // Dump the header include information we are past the predefines buffer or
113 // are showing all headers.
114 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000115 // Write to a temporary string to avoid unnecessary flushing on errs().
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000116 SmallString<512> Filename(UserLoc.getFilename());
Hans Wennborg0fd62072013-08-09 00:32:23 +0000117 if (!MSStyle)
118 Lexer::Stringify(Filename);
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000119
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000120 SmallString<256> Msg;
Hans Wennborg0fd62072013-08-09 00:32:23 +0000121 if (MSStyle)
122 Msg += "Note: including file:";
123
Daniel Dunbarfe908a82011-03-21 19:37:38 +0000124 if (ShowDepth) {
Sebastian Redl4e521232011-04-14 14:07:45 +0000125 // The main source file is at depth 1, so skip one dot.
126 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Hans Wennborg0fd62072013-08-09 00:32:23 +0000127 Msg += MSStyle ? ' ' : '.';
128
129 if (!MSStyle)
130 Msg += ' ';
Daniel Dunbarfe908a82011-03-21 19:37:38 +0000131 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000132 Msg += Filename;
133 Msg += '\n';
134
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000135 OutputFile->write(Msg.data(), Msg.size());
Ehsan Akhgaribd927b62014-07-17 19:08:19 +0000136 OutputFile->flush();
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000137 }
138}