File indexing completed on 2026-05-10 08:36:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_CLANG_FRONTEND_UTILS_H
0014 #define LLVM_CLANG_FRONTEND_UTILS_H
0015
0016 #include "clang/Basic/Diagnostic.h"
0017 #include "clang/Basic/LLVM.h"
0018 #include "clang/Driver/OptionUtils.h"
0019 #include "clang/Frontend/DependencyOutputOptions.h"
0020 #include "llvm/ADT/ArrayRef.h"
0021 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0022 #include "llvm/ADT/StringMap.h"
0023 #include "llvm/ADT/StringRef.h"
0024 #include "llvm/ADT/StringSet.h"
0025 #include "llvm/Support/FileCollector.h"
0026 #include "llvm/Support/VirtualFileSystem.h"
0027 #include <cstdint>
0028 #include <memory>
0029 #include <string>
0030 #include <system_error>
0031 #include <utility>
0032 #include <vector>
0033
0034 namespace clang {
0035
0036 class ASTReader;
0037 class CompilerInstance;
0038 class CompilerInvocation;
0039 class DiagnosticsEngine;
0040 class ExternalSemaSource;
0041 class FrontendOptions;
0042 class PCHContainerReader;
0043 class Preprocessor;
0044 class PreprocessorOptions;
0045 class PreprocessorOutputOptions;
0046 class CodeGenOptions;
0047
0048
0049
0050 void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
0051 const PCHContainerReader &PCHContainerRdr,
0052 const FrontendOptions &FEOpts,
0053 const CodeGenOptions &CodeGenOpts);
0054
0055
0056 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
0057 const PreprocessorOutputOptions &Opts);
0058
0059
0060
0061
0062
0063 class DependencyCollector {
0064 public:
0065 virtual ~DependencyCollector();
0066
0067 virtual void attachToPreprocessor(Preprocessor &PP);
0068 virtual void attachToASTReader(ASTReader &R);
0069 ArrayRef<std::string> getDependencies() const { return Dependencies; }
0070
0071
0072
0073
0074
0075 virtual bool sawDependency(StringRef Filename, bool FromModule,
0076 bool IsSystem, bool IsModuleFile, bool IsMissing);
0077
0078
0079 virtual void finishedMainFile(DiagnosticsEngine &Diags) {}
0080
0081
0082 virtual bool needSystemDependencies() { return false; }
0083
0084
0085
0086 virtual void maybeAddDependency(StringRef Filename, bool FromModule,
0087 bool IsSystem, bool IsModuleFile,
0088 bool IsMissing);
0089
0090 protected:
0091
0092
0093 bool addDependency(StringRef Filename);
0094
0095 private:
0096 llvm::StringSet<> Seen;
0097 std::vector<std::string> Dependencies;
0098 };
0099
0100
0101
0102
0103
0104 class DependencyFileGenerator : public DependencyCollector {
0105 public:
0106 DependencyFileGenerator(const DependencyOutputOptions &Opts);
0107
0108 void attachToPreprocessor(Preprocessor &PP) override;
0109
0110 void finishedMainFile(DiagnosticsEngine &Diags) override;
0111
0112 bool needSystemDependencies() final { return IncludeSystemHeaders; }
0113
0114 bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
0115 bool IsModuleFile, bool IsMissing) final;
0116
0117 protected:
0118 void outputDependencyFile(llvm::raw_ostream &OS);
0119
0120 private:
0121 void outputDependencyFile(DiagnosticsEngine &Diags);
0122
0123 std::string OutputFile;
0124 std::vector<std::string> Targets;
0125 bool IncludeSystemHeaders;
0126 bool PhonyTarget;
0127 bool AddMissingHeaderDeps;
0128 bool SeenMissingHeader;
0129 bool IncludeModuleFiles;
0130 DependencyOutputFormat OutputFormat;
0131 unsigned InputFileIndex;
0132 };
0133
0134
0135
0136 class ModuleDependencyCollector : public DependencyCollector {
0137 std::string DestDir;
0138 bool HasErrors = false;
0139 llvm::StringSet<> Seen;
0140 llvm::vfs::YAMLVFSWriter VFSWriter;
0141 llvm::FileCollector::PathCanonicalizer Canonicalizer;
0142
0143 std::error_code copyToRoot(StringRef Src, StringRef Dst = {});
0144
0145 public:
0146 ModuleDependencyCollector(std::string DestDir)
0147 : DestDir(std::move(DestDir)) {}
0148 ~ModuleDependencyCollector() override { writeFileMap(); }
0149
0150 StringRef getDest() { return DestDir; }
0151 virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
0152 virtual void addFile(StringRef Filename, StringRef FileDst = {});
0153
0154 virtual void addFileMapping(StringRef VPath, StringRef RPath) {
0155 VFSWriter.addFileMapping(VPath, RPath);
0156 }
0157
0158 void attachToPreprocessor(Preprocessor &PP) override;
0159 void attachToASTReader(ASTReader &R) override;
0160
0161 virtual void writeFileMap();
0162 virtual bool hasErrors() { return HasErrors; }
0163 };
0164
0165
0166
0167 void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
0168 StringRef SysRoot);
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 void AttachHeaderIncludeGen(Preprocessor &PP,
0183 const DependencyOutputOptions &DepOpts,
0184 bool ShowAllHeaders = false,
0185 StringRef OutputPath = {},
0186 bool ShowDepth = true, bool MSStyle = false);
0187
0188
0189
0190 IntrusiveRefCntPtr<ExternalSemaSource>
0191 createChainedIncludesSource(CompilerInstance &CI,
0192 IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
0193
0194
0195 struct CreateInvocationOptions {
0196
0197
0198 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = nullptr;
0199
0200
0201
0202 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr;
0203
0204
0205
0206 bool RecoverOnError = false;
0207
0208
0209
0210
0211 bool ProbePrecompiled = false;
0212
0213
0214 std::vector<std::string> *CC1Args = nullptr;
0215 };
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 std::unique_ptr<CompilerInvocation>
0236 createInvocation(ArrayRef<const char *> Args,
0237 CreateInvocationOptions Opts = {});
0238
0239 }
0240
0241 #endif