clang 23.0.0git
APINotesManager.h
Go to the documentation of this file.
1//===--- APINotesManager.h - Manage API Notes Files -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_APINOTES_APINOTESMANAGER_H
10#define LLVM_CLANG_APINOTES_APINOTESMANAGER_H
11
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/PointerUnion.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/VersionTuple.h"
18#include <memory>
19#include <string>
20
21namespace clang {
22
23class DirectoryEntry;
24class FileEntry;
25class LangOptions;
26class Module;
27class SourceManager;
28
29namespace api_notes {
30
31class APINotesReader;
32
33/// The API notes manager helps find API notes associated with declarations.
34///
35/// API notes are externally-provided annotations for declarations that can
36/// introduce new attributes (covering availability, nullability of
37/// parameters/results, and so on) for specific declarations without directly
38/// modifying the headers that contain those declarations.
39///
40/// The API notes manager is responsible for finding and loading the
41/// external API notes files that correspond to a given header. Its primary
42/// operation is \c findAPINotes(), which finds the API notes reader that
43/// provides information about the declarations at that location.
45 using ReaderEntry = llvm::PointerUnion<DirectoryEntryRef, APINotesReader *>;
46
47 SourceManager &SM;
48
49 /// Whether to implicitly search for API notes files based on the
50 /// source file from which an entity was declared.
51 bool ImplicitAPINotes;
52
53 /// Cached value of hasAPINotes() true once any current-module reader has
54 /// been loaded, or if implicit API notes lookup is enabled. Monotonic within
55 /// a compilation, so it can be tested per-declaration without recomputing.
56 bool HasAPINotes;
57
58 /// Whether to apply all APINotes as optionally-applied versioned
59 /// entities. This means that when building a Clang module,
60 /// we capture every note on a given decl wrapped in a SwiftVersionedAttr
61 /// (with an empty version field for unversioned notes), and have the
62 /// client apply the relevant version's notes.
63 bool VersionIndependentSwift;
64
65 /// The Swift version to use when interpreting versioned API notes.
66 llvm::VersionTuple SwiftVersion;
67
68 enum ReaderKind : unsigned { Public = 0, Private = 1 };
69
70 /// API notes readers for the current module.
71 ///
72 /// There can be up to two of these, one for public headers and one
73 /// for private headers.
74 ///
75 /// Not using std::unique_ptr to store these, since the reader pointers are
76 /// also stored in llvm::PointerUnion below.
77 APINotesReader *CurrentModuleReaders[2] = {nullptr, nullptr};
78
79 /// A mapping from header file directories to the API notes reader for
80 /// that directory, or a redirection to another directory entry that may
81 /// have more information, or NULL to indicate that there is no API notes
82 /// reader for this directory.
83 llvm::DenseMap<const DirectoryEntry *, ReaderEntry> Readers;
84
85 /// Load the API notes associated with the given file, whether it is
86 /// the binary or source form of API notes.
87 ///
88 /// \returns the API notes reader for this file, or null if there is
89 /// a failure.
90 std::unique_ptr<APINotesReader> loadAPINotes(FileEntryRef APINotesFile);
91
92 /// Load the API notes associated with the given buffer, whether it is
93 /// the binary or source form of API notes.
94 ///
95 /// \returns the API notes reader for this file, or null if there is
96 /// a failure.
97 std::unique_ptr<APINotesReader> loadAPINotes(StringRef Buffer);
98
99 /// Load the given API notes file for the given header directory.
100 ///
101 /// \param HeaderDir The directory at which we
102 ///
103 /// \returns true if an error occurred.
104 bool loadAPINotes(const DirectoryEntry *HeaderDir, FileEntryRef APINotesFile);
105
106 /// Look for API notes in the given directory.
107 ///
108 /// This might find either a binary or source API notes.
109 OptionalFileEntryRef findAPINotesFile(DirectoryEntryRef Directory,
110 StringRef FileName,
111 bool WantPublic = true);
112
113 /// Attempt to load API notes for the given framework. A framework will have
114 /// the API notes file under either {FrameworkPath}/APINotes,
115 /// {FrameworkPath}/Headers or {FrameworkPath}/PrivateHeaders, while a
116 /// library will have the API notes simply in its directory.
117 ///
118 /// \param FrameworkPath The path to the framework.
119 /// \param Public Whether to load the public API notes. Otherwise, attempt
120 /// to load the private API notes.
121 ///
122 /// \returns the header directory entry (e.g., for Headers or PrivateHeaders)
123 /// for which the API notes were successfully loaded, or NULL if API notes
124 /// could not be loaded for any reason.
125 OptionalDirectoryEntryRef loadFrameworkAPINotes(llvm::StringRef FrameworkPath,
126 llvm::StringRef FrameworkName,
127 bool Public);
128
129public:
130 APINotesManager(SourceManager &SM, const LangOptions &LangOpts);
132
133 /// Set the Swift version to use when filtering API notes.
134 void setSwiftVersion(llvm::VersionTuple Version) {
135 this->SwiftVersion = Version;
136 }
137
138 /// Load the API notes for the current module.
139 ///
140 /// \param M The current module.
141 /// \param LookInModule Whether to look inside the module itself.
142 /// \param SearchPaths The paths in which we should search for API notes
143 /// for the current module.
144 ///
145 /// \returns true if API notes were successfully loaded, \c false otherwise.
146 bool loadCurrentModuleAPINotes(Module *M, bool LookInModule,
147 ArrayRef<std::string> SearchPaths);
148
149 /// Get FileEntry for the APINotes of the module that is currently being
150 /// compiled.
151 ///
152 /// \param M The current module.
153 /// \param LookInModule Whether to look inside the directory of the current
154 /// module.
155 /// \param SearchPaths The paths in which we should search for API
156 /// notes for the current module.
157 ///
158 /// \returns a vector of FileEntry where APINotes files are.
160 getCurrentModuleAPINotes(Module *M, bool LookInModule,
161 ArrayRef<std::string> SearchPaths);
162
163 /// Load Compiled API notes for current module.
164 ///
165 /// \param Buffers Array of compiled API notes.
166 ///
167 /// \returns true if API notes were successfully loaded, \c false otherwise.
169
170 /// Retrieve the set of API notes readers for the current module.
172 bool HasPublic = CurrentModuleReaders[ReaderKind::Public];
173 bool HasPrivate = CurrentModuleReaders[ReaderKind::Private];
174 assert((!HasPrivate || HasPublic) && "private module requires public module");
175 if (!HasPrivate && !HasPublic)
176 return {};
177 return ArrayRef(CurrentModuleReaders).slice(0, HasPrivate ? 2 : 1);
178 }
179
180 bool hasAPINotes() const { return HasAPINotes; }
181
182 /// Find the API notes readers that correspond to the given source location.
184
185 bool captureVersionIndependentSwift() { return VersionIndependentSwift; }
186};
187
188} // end namespace api_notes
189} // end namespace clang
190
191#endif
Defines the clang::SourceLocation class and associated facilities.
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:273
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes a module or submodule.
Definition Module.h:340
Encodes a location in the source.
This class handles loading and caching of source files into memory.
APINotesManager(SourceManager &SM, const LangOptions &LangOpts)
llvm::SmallVector< FileEntryRef, 2 > getCurrentModuleAPINotes(Module *M, bool LookInModule, ArrayRef< std::string > SearchPaths)
Get FileEntry for the APINotes of the module that is currently being compiled.
ArrayRef< APINotesReader * > getCurrentModuleReaders() const
Retrieve the set of API notes readers for the current module.
bool loadCurrentModuleAPINotesFromBuffer(ArrayRef< StringRef > Buffers)
Load Compiled API notes for current module.
void setSwiftVersion(llvm::VersionTuple Version)
Set the Swift version to use when filtering API notes.
llvm::SmallVector< APINotesReader *, 2 > findAPINotes(SourceLocation Loc)
Find the API notes readers that correspond to the given source location.
bool loadCurrentModuleAPINotes(Module *M, bool LookInModule, ArrayRef< std::string > SearchPaths)
Load the API notes for the current module.
A class that reads API notes data from a binary file that was written by the APINotesWriter.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:196
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef