|
||||
File indexing completed on 2025-01-31 10:12:00
0001 // Protocol Buffers - Google's data interchange format 0002 // Copyright 2008 Google Inc. All rights reserved. 0003 // 0004 // Use of this source code is governed by a BSD-style 0005 // license that can be found in the LICENSE file or at 0006 // https://developers.google.com/open-source/licenses/bsd 0007 0008 // Author: kenton@google.com (Kenton Varda) 0009 // Based on original Protocol Buffers design by 0010 // Sanjay Ghemawat, Jeff Dean, and others. 0011 // 0012 // This file is the public interface to the .proto file parser. 0013 0014 #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ 0015 #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ 0016 0017 #include <string> 0018 #include <utility> 0019 #include <vector> 0020 0021 #include "absl/strings/string_view.h" 0022 #include "google/protobuf/compiler/parser.h" 0023 #include "google/protobuf/descriptor.h" 0024 #include "google/protobuf/descriptor_database.h" 0025 0026 // Must be included last. 0027 #include "google/protobuf/port_def.inc" 0028 0029 namespace google { 0030 namespace protobuf { 0031 0032 namespace io { 0033 class ZeroCopyInputStream; 0034 } 0035 0036 namespace compiler { 0037 0038 // Defined in this file. 0039 class Importer; 0040 class MultiFileErrorCollector; 0041 class SourceTree; 0042 class DiskSourceTree; 0043 0044 // TODO: Move all SourceTree stuff to a separate file? 0045 0046 // An implementation of DescriptorDatabase which loads files from a SourceTree 0047 // and parses them. 0048 // 0049 // Note: This class is not thread-safe since it maintains a table of source 0050 // code locations for error reporting. However, when a DescriptorPool wraps 0051 // a DescriptorDatabase, it uses mutex locking to make sure only one method 0052 // of the database is called at a time, even if the DescriptorPool is used 0053 // from multiple threads. Therefore, there is only a problem if you create 0054 // multiple DescriptorPools wrapping the same SourceTreeDescriptorDatabase 0055 // and use them from multiple threads. 0056 // 0057 // Note: This class does not implement FindFileContainingSymbol() or 0058 // FindFileContainingExtension(); these will always return false. 0059 class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase { 0060 public: 0061 SourceTreeDescriptorDatabase(SourceTree* source_tree); 0062 0063 // If non-NULL, fallback_database will be checked if a file doesn't exist in 0064 // the specified source_tree. 0065 SourceTreeDescriptorDatabase(SourceTree* source_tree, 0066 DescriptorDatabase* fallback_database); 0067 ~SourceTreeDescriptorDatabase() override; 0068 0069 // Instructs the SourceTreeDescriptorDatabase to report any parse errors 0070 // to the given MultiFileErrorCollector. This should be called before 0071 // parsing. error_collector must remain valid until either this method 0072 // is called again or the SourceTreeDescriptorDatabase is destroyed. 0073 void RecordErrorsTo(MultiFileErrorCollector* error_collector) { 0074 error_collector_ = error_collector; 0075 } 0076 0077 // Gets a DescriptorPool::ErrorCollector which records errors to the 0078 // MultiFileErrorCollector specified with RecordErrorsTo(). This collector 0079 // has the ability to determine exact line and column numbers of errors 0080 // from the information given to it by the DescriptorPool. 0081 DescriptorPool::ErrorCollector* GetValidationErrorCollector() { 0082 using_validation_error_collector_ = true; 0083 return &validation_error_collector_; 0084 } 0085 0086 // implements DescriptorDatabase ----------------------------------- 0087 bool FindFileByName(const std::string& filename, 0088 FileDescriptorProto* output) override; 0089 bool FindFileContainingSymbol(const std::string& symbol_name, 0090 FileDescriptorProto* output) override; 0091 bool FindFileContainingExtension(const std::string& containing_type, 0092 int field_number, 0093 FileDescriptorProto* output) override; 0094 0095 private: 0096 class SingleFileErrorCollector; 0097 0098 SourceTree* source_tree_; 0099 DescriptorDatabase* fallback_database_; 0100 MultiFileErrorCollector* error_collector_; 0101 0102 class PROTOBUF_EXPORT ValidationErrorCollector 0103 : public DescriptorPool::ErrorCollector { 0104 public: 0105 ValidationErrorCollector(SourceTreeDescriptorDatabase* owner); 0106 ~ValidationErrorCollector() override; 0107 0108 // implements ErrorCollector --------------------------------------- 0109 void RecordError(absl::string_view filename, absl::string_view element_name, 0110 const Message* descriptor, ErrorLocation location, 0111 absl::string_view message) override; 0112 0113 void RecordWarning(absl::string_view filename, 0114 absl::string_view element_name, 0115 const Message* descriptor, ErrorLocation location, 0116 absl::string_view message) override; 0117 0118 private: 0119 SourceTreeDescriptorDatabase* owner_; 0120 }; 0121 friend class ValidationErrorCollector; 0122 0123 bool using_validation_error_collector_; 0124 SourceLocationTable source_locations_; 0125 ValidationErrorCollector validation_error_collector_; 0126 }; 0127 0128 // Simple interface for parsing .proto files. This wraps the process 0129 // of opening the file, parsing it with a Parser, recursively parsing all its 0130 // imports, and then cross-linking the results to produce a FileDescriptor. 0131 // 0132 // This is really just a thin wrapper around SourceTreeDescriptorDatabase. 0133 // You may find that SourceTreeDescriptorDatabase is more flexible. 0134 // 0135 // TODO: I feel like this class is not well-named. 0136 class PROTOBUF_EXPORT Importer { 0137 public: 0138 Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector); 0139 Importer(const Importer&) = delete; 0140 Importer& operator=(const Importer&) = delete; 0141 ~Importer(); 0142 0143 // Import the given file and build a FileDescriptor representing it. If 0144 // the file is already in the DescriptorPool, the existing FileDescriptor 0145 // will be returned. The FileDescriptor is property of the DescriptorPool, 0146 // and will remain valid until it is destroyed. If any errors occur, they 0147 // will be reported using the error collector and Import() will return NULL. 0148 // 0149 // A particular Importer object will only report errors for a particular 0150 // file once. All future attempts to import the same file will return NULL 0151 // without reporting any errors. The idea is that you might want to import 0152 // a lot of files without seeing the same errors over and over again. If 0153 // you want to see errors for the same files repeatedly, you can use a 0154 // separate Importer object to import each one (but use the same 0155 // DescriptorPool so that they can be cross-linked). 0156 const FileDescriptor* Import(const std::string& filename); 0157 0158 // The DescriptorPool in which all imported FileDescriptors and their 0159 // contents are stored. 0160 inline const DescriptorPool* pool() const { return &pool_; } 0161 0162 void AddUnusedImportTrackFile(const std::string& file_name, 0163 bool is_error = false); 0164 void ClearUnusedImportTrackFiles(); 0165 0166 0167 private: 0168 SourceTreeDescriptorDatabase database_; 0169 DescriptorPool pool_; 0170 }; 0171 0172 // If the importer encounters problems while trying to import the proto files, 0173 // it reports them to a MultiFileErrorCollector. 0174 class PROTOBUF_EXPORT MultiFileErrorCollector { 0175 public: 0176 MultiFileErrorCollector() {} 0177 MultiFileErrorCollector(const MultiFileErrorCollector&) = delete; 0178 MultiFileErrorCollector& operator=(const MultiFileErrorCollector&) = delete; 0179 virtual ~MultiFileErrorCollector(); 0180 0181 // Line and column numbers are zero-based. A line number of -1 indicates 0182 // an error with the entire file (e.g. "not found"). 0183 virtual void RecordError(absl::string_view filename, int line, int column, 0184 absl::string_view message) 0185 = 0; 0186 virtual void RecordWarning(absl::string_view filename, int line, int column, 0187 absl::string_view message) { 0188 } 0189 0190 }; 0191 0192 // Abstract interface which represents a directory tree containing proto files. 0193 // Used by the default implementation of Importer to resolve import statements 0194 // Most users will probably want to use the DiskSourceTree implementation, 0195 // below. 0196 class PROTOBUF_EXPORT SourceTree { 0197 public: 0198 SourceTree() {} 0199 SourceTree(const SourceTree&) = delete; 0200 SourceTree& operator=(const SourceTree&) = delete; 0201 virtual ~SourceTree(); 0202 0203 // Open the given file and return a stream that reads it, or NULL if not 0204 // found. The caller takes ownership of the returned object. The filename 0205 // must be a path relative to the root of the source tree and must not 0206 // contain "." or ".." components. 0207 virtual io::ZeroCopyInputStream* Open(absl::string_view filename) = 0; 0208 0209 // If Open() returns NULL, calling this method immediately will return an 0210 // description of the error. 0211 // Subclasses should implement this method and return a meaningful value for 0212 // better error reporting. 0213 // TODO: change this to a pure virtual function. 0214 virtual std::string GetLastErrorMessage(); 0215 }; 0216 0217 // An implementation of SourceTree which loads files from locations on disk. 0218 // Multiple mappings can be set up to map locations in the DiskSourceTree to 0219 // locations in the physical filesystem. 0220 class PROTOBUF_EXPORT DiskSourceTree : public SourceTree { 0221 public: 0222 DiskSourceTree(); 0223 DiskSourceTree(const DiskSourceTree&) = delete; 0224 DiskSourceTree& operator=(const DiskSourceTree&) = delete; 0225 ~DiskSourceTree() override; 0226 0227 // Map a path on disk to a location in the SourceTree. The path may be 0228 // either a file or a directory. If it is a directory, the entire tree 0229 // under it will be mapped to the given virtual location. To map a directory 0230 // to the root of the source tree, pass an empty string for virtual_path. 0231 // 0232 // If multiple mapped paths apply when opening a file, they will be searched 0233 // in order. For example, if you do: 0234 // MapPath("bar", "foo/bar"); 0235 // MapPath("", "baz"); 0236 // and then you do: 0237 // Open("bar/qux"); 0238 // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux, 0239 // returning the first one that opens successfully. 0240 // 0241 // disk_path may be an absolute path or relative to the current directory, 0242 // just like a path you'd pass to open(). 0243 void MapPath(absl::string_view virtual_path, absl::string_view disk_path); 0244 0245 // Return type for DiskFileToVirtualFile(). 0246 enum DiskFileToVirtualFileResult { 0247 SUCCESS, 0248 SHADOWED, 0249 CANNOT_OPEN, 0250 NO_MAPPING 0251 }; 0252 0253 // Given a path to a file on disk, find a virtual path mapping to that 0254 // file. The first mapping created with MapPath() whose disk_path contains 0255 // the filename is used. However, that virtual path may not actually be 0256 // usable to open the given file. Possible return values are: 0257 // * SUCCESS: The mapping was found. *virtual_file is filled in so that 0258 // calling Open(*virtual_file) will open the file named by disk_file. 0259 // * SHADOWED: A mapping was found, but using Open() to open this virtual 0260 // path will end up returning some different file. This is because some 0261 // other mapping with a higher precedence also matches this virtual path 0262 // and maps it to a different file that exists on disk. *virtual_file 0263 // is filled in as it would be in the SUCCESS case. *shadowing_disk_file 0264 // is filled in with the disk path of the file which would be opened if 0265 // you were to call Open(*virtual_file). 0266 // * CANNOT_OPEN: The mapping was found and was not shadowed, but the 0267 // file specified cannot be opened. When this value is returned, 0268 // errno will indicate the reason the file cannot be opened. *virtual_file 0269 // will be set to the virtual path as in the SUCCESS case, even though 0270 // it is not useful. 0271 // * NO_MAPPING: Indicates that no mapping was found which contains this 0272 // file. 0273 DiskFileToVirtualFileResult DiskFileToVirtualFile( 0274 absl::string_view disk_file, std::string* virtual_file, 0275 std::string* shadowing_disk_file); 0276 0277 // Given a virtual path, find the path to the file on disk. 0278 // Return true and update disk_file with the on-disk path if the file exists. 0279 // Return false and leave disk_file untouched if the file doesn't exist. 0280 bool VirtualFileToDiskFile(absl::string_view virtual_file, 0281 std::string* disk_file); 0282 0283 // implements SourceTree ------------------------------------------- 0284 io::ZeroCopyInputStream* Open(absl::string_view filename) override; 0285 0286 std::string GetLastErrorMessage() override; 0287 0288 private: 0289 struct Mapping { 0290 std::string virtual_path; 0291 std::string disk_path; 0292 0293 inline Mapping(std::string virtual_path_param, std::string disk_path_param) 0294 : virtual_path(std::move(virtual_path_param)), 0295 disk_path(std::move(disk_path_param)) {} 0296 }; 0297 std::vector<Mapping> mappings_; 0298 std::string last_error_message_; 0299 0300 // Like Open(), but returns the on-disk path in disk_file if disk_file is 0301 // non-NULL and the file could be successfully opened. 0302 io::ZeroCopyInputStream* OpenVirtualFile(absl::string_view virtual_file, 0303 std::string* disk_file); 0304 0305 // Like Open() but given the actual on-disk path. 0306 io::ZeroCopyInputStream* OpenDiskFile(absl::string_view filename); 0307 }; 0308 0309 } // namespace compiler 0310 } // namespace protobuf 0311 } // namespace google 0312 0313 #include "google/protobuf/port_undef.inc" 0314 0315 #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |