xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/HeaderIncludeGen.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
12f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
13f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
14f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
15f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
16f4a2713aSLionel Sambuc using namespace clang;
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc namespace {
19f4a2713aSLionel Sambuc class HeaderIncludesCallback : public PPCallbacks {
20f4a2713aSLionel Sambuc   SourceManager &SM;
21f4a2713aSLionel Sambuc   raw_ostream *OutputFile;
22f4a2713aSLionel Sambuc   unsigned CurrentIncludeDepth;
23f4a2713aSLionel Sambuc   bool HasProcessedPredefines;
24f4a2713aSLionel Sambuc   bool OwnsOutputFile;
25f4a2713aSLionel Sambuc   bool ShowAllHeaders;
26f4a2713aSLionel Sambuc   bool ShowDepth;
27f4a2713aSLionel Sambuc   bool MSStyle;
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc public:
HeaderIncludesCallback(const Preprocessor * PP,bool ShowAllHeaders_,raw_ostream * OutputFile_,bool OwnsOutputFile_,bool ShowDepth_,bool MSStyle_)30f4a2713aSLionel Sambuc   HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
31f4a2713aSLionel Sambuc                          raw_ostream *OutputFile_, bool OwnsOutputFile_,
32f4a2713aSLionel Sambuc                          bool ShowDepth_, bool MSStyle_)
33f4a2713aSLionel Sambuc     : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34f4a2713aSLionel Sambuc       CurrentIncludeDepth(0), HasProcessedPredefines(false),
35f4a2713aSLionel Sambuc       OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
36f4a2713aSLionel Sambuc       ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
37f4a2713aSLionel Sambuc 
~HeaderIncludesCallback()38f4a2713aSLionel Sambuc   ~HeaderIncludesCallback() {
39f4a2713aSLionel Sambuc     if (OwnsOutputFile)
40f4a2713aSLionel Sambuc       delete OutputFile;
41f4a2713aSLionel Sambuc   }
42f4a2713aSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44f4a2713aSLionel Sambuc                    SrcMgr::CharacteristicKind FileType,
45*0a6a1f1dSLionel Sambuc                    FileID PrevFID) override;
46f4a2713aSLionel Sambuc };
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
AttachHeaderIncludeGen(Preprocessor & PP,bool ShowAllHeaders,StringRef OutputPath,bool ShowDepth,bool MSStyle)49f4a2713aSLionel Sambuc void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
50f4a2713aSLionel Sambuc                                    StringRef OutputPath, bool ShowDepth,
51f4a2713aSLionel Sambuc                                    bool MSStyle) {
52*0a6a1f1dSLionel Sambuc   raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
53f4a2713aSLionel Sambuc   bool OwnsOutputFile = false;
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc   // Open the output file, if used.
56f4a2713aSLionel Sambuc   if (!OutputPath.empty()) {
57*0a6a1f1dSLionel Sambuc     std::error_code EC;
58f4a2713aSLionel Sambuc     llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
59*0a6a1f1dSLionel Sambuc         OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
60*0a6a1f1dSLionel Sambuc     if (EC) {
61*0a6a1f1dSLionel Sambuc       PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
62*0a6a1f1dSLionel Sambuc           << EC.message();
63f4a2713aSLionel Sambuc       delete OS;
64f4a2713aSLionel Sambuc     } else {
65f4a2713aSLionel Sambuc       OS->SetUnbuffered();
66f4a2713aSLionel Sambuc       OS->SetUseAtomicWrites(true);
67f4a2713aSLionel Sambuc       OutputFile = OS;
68f4a2713aSLionel Sambuc       OwnsOutputFile = true;
69f4a2713aSLionel Sambuc     }
70f4a2713aSLionel Sambuc   }
71f4a2713aSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc   PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
73*0a6a1f1dSLionel Sambuc                                                               ShowAllHeaders,
74*0a6a1f1dSLionel Sambuc                                                               OutputFile,
75*0a6a1f1dSLionel Sambuc                                                               OwnsOutputFile,
76*0a6a1f1dSLionel Sambuc                                                               ShowDepth,
77*0a6a1f1dSLionel Sambuc                                                               MSStyle));
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind NewFileType,FileID PrevFID)80f4a2713aSLionel Sambuc void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
81f4a2713aSLionel Sambuc                                          FileChangeReason Reason,
82f4a2713aSLionel Sambuc                                        SrcMgr::CharacteristicKind NewFileType,
83f4a2713aSLionel Sambuc                                        FileID PrevFID) {
84f4a2713aSLionel Sambuc   // Unless we are exiting a #include, make sure to skip ahead to the line the
85f4a2713aSLionel Sambuc   // #include directive was at.
86f4a2713aSLionel Sambuc   PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
87f4a2713aSLionel Sambuc   if (UserLoc.isInvalid())
88f4a2713aSLionel Sambuc     return;
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc   // Adjust the current include depth.
91f4a2713aSLionel Sambuc   if (Reason == PPCallbacks::EnterFile) {
92f4a2713aSLionel Sambuc     ++CurrentIncludeDepth;
93f4a2713aSLionel Sambuc   } else if (Reason == PPCallbacks::ExitFile) {
94f4a2713aSLionel Sambuc     if (CurrentIncludeDepth)
95f4a2713aSLionel Sambuc       --CurrentIncludeDepth;
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc     // We track when we are done with the predefines by watching for the first
98f4a2713aSLionel Sambuc     // place where we drop back to a nesting depth of 1.
99f4a2713aSLionel Sambuc     if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
100f4a2713aSLionel Sambuc       HasProcessedPredefines = true;
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc     return;
103f4a2713aSLionel Sambuc   } else
104f4a2713aSLionel Sambuc     return;
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   // Show the header if we are (a) past the predefines, or (b) showing all
107f4a2713aSLionel Sambuc   // headers and in the predefines at a depth past the initial file and command
108f4a2713aSLionel Sambuc   // line buffers.
109f4a2713aSLionel Sambuc   bool ShowHeader = (HasProcessedPredefines ||
110f4a2713aSLionel Sambuc                      (ShowAllHeaders && CurrentIncludeDepth > 2));
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   // Dump the header include information we are past the predefines buffer or
113f4a2713aSLionel Sambuc   // are showing all headers.
114f4a2713aSLionel Sambuc   if (ShowHeader && Reason == PPCallbacks::EnterFile) {
115f4a2713aSLionel Sambuc     // Write to a temporary string to avoid unnecessary flushing on errs().
116f4a2713aSLionel Sambuc     SmallString<512> Filename(UserLoc.getFilename());
117f4a2713aSLionel Sambuc     if (!MSStyle)
118f4a2713aSLionel Sambuc       Lexer::Stringify(Filename);
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc     SmallString<256> Msg;
121f4a2713aSLionel Sambuc     if (MSStyle)
122f4a2713aSLionel Sambuc       Msg += "Note: including file:";
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc     if (ShowDepth) {
125f4a2713aSLionel Sambuc       // The main source file is at depth 1, so skip one dot.
126f4a2713aSLionel Sambuc       for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
127f4a2713aSLionel Sambuc         Msg += MSStyle ? ' ' : '.';
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc       if (!MSStyle)
130f4a2713aSLionel Sambuc         Msg += ' ';
131f4a2713aSLionel Sambuc     }
132f4a2713aSLionel Sambuc     Msg += Filename;
133f4a2713aSLionel Sambuc     Msg += '\n';
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc     OutputFile->write(Msg.data(), Msg.size());
136*0a6a1f1dSLionel Sambuc     OutputFile->flush();
137f4a2713aSLionel Sambuc   }
138f4a2713aSLionel Sambuc }
139