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