1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "clang/Frontend/DependencyOutputOptions.h" |
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 : public PPCallbacks { |
20 | SourceManager &; |
21 | raw_ostream *; |
22 | const DependencyOutputOptions &; |
23 | unsigned ; |
24 | bool ; |
25 | bool ; |
26 | bool ; |
27 | bool ; |
28 | bool ; |
29 | |
30 | public: |
31 | HeaderIncludesCallback(const Preprocessor *PP, bool , |
32 | raw_ostream *OutputFile_, |
33 | const DependencyOutputOptions &DepOpts, |
34 | bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_) |
35 | : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts), |
36 | CurrentIncludeDepth(0), HasProcessedPredefines(false), |
37 | OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), |
38 | ShowDepth(ShowDepth_), MSStyle(MSStyle_) {} |
39 | |
40 | ~HeaderIncludesCallback() override { |
41 | if (OwnsOutputFile) |
42 | delete OutputFile; |
43 | } |
44 | |
45 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
46 | SrcMgr::CharacteristicKind FileType, |
47 | FileID PrevFID) override; |
48 | }; |
49 | } |
50 | |
51 | static void (raw_ostream *OutputFile, StringRef Filename, |
52 | bool ShowDepth, unsigned CurrentIncludeDepth, |
53 | bool MSStyle) { |
54 | |
55 | SmallString<512> Pathname(Filename); |
56 | if (!MSStyle) |
57 | Lexer::Stringify(Pathname); |
58 | |
59 | SmallString<256> Msg; |
60 | if (MSStyle) |
61 | Msg += "Note: including file:"; |
62 | |
63 | if (ShowDepth) { |
64 | |
65 | for (unsigned i = 1; i != CurrentIncludeDepth; ++i) |
66 | Msg += MSStyle ? ' ' : '.'; |
67 | |
68 | if (!MSStyle) |
69 | Msg += ' '; |
70 | } |
71 | Msg += Pathname; |
72 | Msg += '\n'; |
73 | |
74 | *OutputFile << Msg; |
75 | OutputFile->flush(); |
76 | } |
77 | |
78 | void clang::(Preprocessor &PP, |
79 | const DependencyOutputOptions &DepOpts, |
80 | bool , StringRef OutputPath, |
81 | bool ShowDepth, bool MSStyle) { |
82 | raw_ostream *OutputFile = &llvm::errs(); |
83 | bool OwnsOutputFile = false; |
84 | |
85 | |
86 | if (MSStyle) { |
87 | switch (DepOpts.ShowIncludesDest) { |
88 | default: |
89 | llvm_unreachable("Invalid destination for /showIncludes output!"); |
90 | case ShowIncludesDestination::Stderr: |
91 | OutputFile = &llvm::errs(); |
92 | break; |
93 | case ShowIncludesDestination::Stdout: |
94 | OutputFile = &llvm::outs(); |
95 | break; |
96 | } |
97 | } |
98 | |
99 | |
100 | if (!OutputPath.empty()) { |
101 | std::error_code EC; |
102 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
103 | OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text); |
104 | if (EC) { |
105 | PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure) |
106 | << EC.message(); |
107 | delete OS; |
108 | } else { |
109 | OS->SetUnbuffered(); |
110 | OutputFile = OS; |
111 | OwnsOutputFile = true; |
112 | } |
113 | } |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | for (const auto & : DepOpts.ExtraDeps) |
121 | PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle); |
122 | PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>( |
123 | &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth, |
124 | MSStyle)); |
125 | } |
126 | |
127 | void HeaderIncludesCallback::(SourceLocation Loc, |
128 | FileChangeReason Reason, |
129 | SrcMgr::CharacteristicKind NewFileType, |
130 | FileID PrevFID) { |
131 | |
132 | |
133 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
134 | if (UserLoc.isInvalid()) |
135 | return; |
136 | |
137 | |
138 | if (Reason == PPCallbacks::EnterFile) { |
139 | ++CurrentIncludeDepth; |
140 | } else if (Reason == PPCallbacks::ExitFile) { |
141 | if (CurrentIncludeDepth) |
142 | --CurrentIncludeDepth; |
143 | |
144 | |
145 | |
146 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) { |
147 | if (!DepOpts.ShowIncludesPretendHeader.empty()) { |
148 | PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader, |
149 | ShowDepth, 2, MSStyle); |
150 | } |
151 | HasProcessedPredefines = true; |
152 | } |
153 | |
154 | return; |
155 | } else |
156 | return; |
157 | |
158 | |
159 | |
160 | |
161 | bool = (HasProcessedPredefines || |
162 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
163 | unsigned IncludeDepth = CurrentIncludeDepth; |
164 | if (!HasProcessedPredefines) |
165 | --IncludeDepth; |
166 | else if (!DepOpts.ShowIncludesPretendHeader.empty()) |
167 | ++IncludeDepth; |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | if (ShowHeader && Reason == PPCallbacks::EnterFile && |
175 | UserLoc.getFilename() != StringRef("<command line>")) { |
176 | PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth, |
177 | MSStyle); |
178 | } |
179 | } |
180 | |