importer.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This file is the public interface to the .proto file parser.
36 
37 #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
38 #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
39 
40 #include <set>
41 #include <string>
42 #include <utility>
43 #include <vector>
47 
48 #include <google/protobuf/port_def.inc>
49 
50 namespace google {
51 namespace protobuf {
52 
53 namespace io {
54 class ZeroCopyInputStream;
55 }
56 
57 namespace compiler {
58 
59 // Defined in this file.
60 class Importer;
61 class MultiFileErrorCollector;
62 class SourceTree;
63 class DiskSourceTree;
64 
65 // TODO(kenton): Move all SourceTree stuff to a separate file?
66 
67 // An implementation of DescriptorDatabase which loads files from a SourceTree
68 // and parses them.
69 //
70 // Note: This class is not thread-safe since it maintains a table of source
71 // code locations for error reporting. However, when a DescriptorPool wraps
72 // a DescriptorDatabase, it uses mutex locking to make sure only one method
73 // of the database is called at a time, even if the DescriptorPool is used
74 // from multiple threads. Therefore, there is only a problem if you create
75 // multiple DescriptorPools wrapping the same SourceTreeDescriptorDatabase
76 // and use them from multiple threads.
77 //
78 // Note: This class does not implement FindFileContainingSymbol() or
79 // FindFileContainingExtension(); these will always return false.
80 class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase {
81  public:
83 
84  // If non-NULL, fallback_database will be checked if a file doesn't exist in
85  // the specified source_tree.
87  DescriptorDatabase* fallback_database);
89 
90  // Instructs the SourceTreeDescriptorDatabase to report any parse errors
91  // to the given MultiFileErrorCollector. This should be called before
92  // parsing. error_collector must remain valid until either this method
93  // is called again or the SourceTreeDescriptorDatabase is destroyed.
94  void RecordErrorsTo(MultiFileErrorCollector* error_collector) {
95  error_collector_ = error_collector;
96  }
97 
98  // Gets a DescriptorPool::ErrorCollector which records errors to the
99  // MultiFileErrorCollector specified with RecordErrorsTo(). This collector
100  // has the ability to determine exact line and column numbers of errors
101  // from the information given to it by the DescriptorPool.
102  DescriptorPool::ErrorCollector* GetValidationErrorCollector() {
103  using_validation_error_collector_ = true;
104  return &validation_error_collector_;
105  }
106 
107  // implements DescriptorDatabase -----------------------------------
108  bool FindFileByName(const std::string& filename,
109  FileDescriptorProto* output) override;
110  bool FindFileContainingSymbol(const std::string& symbol_name,
111  FileDescriptorProto* output) override;
112  bool FindFileContainingExtension(const std::string& containing_type,
113  int field_number,
114  FileDescriptorProto* output) override;
115 
116  private:
118 
122 
123  class PROTOBUF_EXPORT ValidationErrorCollector
124  : public DescriptorPool::ErrorCollector {
125  public:
128 
129  // implements ErrorCollector ---------------------------------------
130  void AddError(const std::string& filename, const std::string& element_name,
132  const std::string& message) override;
133 
134  void AddWarning(const std::string& filename,
137  const std::string& message) override;
138 
139  private:
141  };
143 
147 };
148 
149 // Simple interface for parsing .proto files. This wraps the process
150 // of opening the file, parsing it with a Parser, recursively parsing all its
151 // imports, and then cross-linking the results to produce a FileDescriptor.
152 //
153 // This is really just a thin wrapper around SourceTreeDescriptorDatabase.
154 // You may find that SourceTreeDescriptorDatabase is more flexible.
155 //
156 // TODO(kenton): I feel like this class is not well-named.
157 class PROTOBUF_EXPORT Importer {
158  public:
159  Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector);
160  ~Importer();
161 
162  // Import the given file and build a FileDescriptor representing it. If
163  // the file is already in the DescriptorPool, the existing FileDescriptor
164  // will be returned. The FileDescriptor is property of the DescriptorPool,
165  // and will remain valid until it is destroyed. If any errors occur, they
166  // will be reported using the error collector and Import() will return NULL.
167  //
168  // A particular Importer object will only report errors for a particular
169  // file once. All future attempts to import the same file will return NULL
170  // without reporting any errors. The idea is that you might want to import
171  // a lot of files without seeing the same errors over and over again. If
172  // you want to see errors for the same files repeatedly, you can use a
173  // separate Importer object to import each one (but use the same
174  // DescriptorPool so that they can be cross-linked).
175  const FileDescriptor* Import(const std::string& filename);
176 
177  // The DescriptorPool in which all imported FileDescriptors and their
178  // contents are stored.
179  inline const DescriptorPool* pool() const { return &pool_; }
180 
181  void AddUnusedImportTrackFile(const std::string& file_name);
182  void ClearUnusedImportTrackFiles();
183 
184 
185  private:
188 
190 };
191 
192 // If the importer encounters problems while trying to import the proto files,
193 // it reports them to a MultiFileErrorCollector.
194 class PROTOBUF_EXPORT MultiFileErrorCollector {
195  public:
197  virtual ~MultiFileErrorCollector();
198 
199  // Line and column numbers are zero-based. A line number of -1 indicates
200  // an error with the entire file (e.g. "not found").
201  virtual void AddError(const std::string& filename, int line, int column,
202  const std::string& message) = 0;
203 
204  virtual void AddWarning(const std::string& filename, int line, int column,
205  const std::string& message) {}
206 
207  private:
209 };
210 
211 // Abstract interface which represents a directory tree containing proto files.
212 // Used by the default implementation of Importer to resolve import statements
213 // Most users will probably want to use the DiskSourceTree implementation,
214 // below.
215 class PROTOBUF_EXPORT SourceTree {
216  public:
217  inline SourceTree() {}
218  virtual ~SourceTree();
219 
220  // Open the given file and return a stream that reads it, or NULL if not
221  // found. The caller takes ownership of the returned object. The filename
222  // must be a path relative to the root of the source tree and must not
223  // contain "." or ".." components.
224  virtual io::ZeroCopyInputStream* Open(const std::string& filename) = 0;
225 
226  // If Open() returns NULL, calling this method immediately will return an
227  // description of the error.
228  // Subclasses should implement this method and return a meaningful value for
229  // better error reporting.
230  // TODO(xiaofeng): change this to a pure virtual function.
231  virtual std::string GetLastErrorMessage();
232 
233  private:
235 };
236 
237 // An implementation of SourceTree which loads files from locations on disk.
238 // Multiple mappings can be set up to map locations in the DiskSourceTree to
239 // locations in the physical filesystem.
240 class PROTOBUF_EXPORT DiskSourceTree : public SourceTree {
241  public:
242  DiskSourceTree();
243  ~DiskSourceTree();
244 
245  // Map a path on disk to a location in the SourceTree. The path may be
246  // either a file or a directory. If it is a directory, the entire tree
247  // under it will be mapped to the given virtual location. To map a directory
248  // to the root of the source tree, pass an empty string for virtual_path.
249  //
250  // If multiple mapped paths apply when opening a file, they will be searched
251  // in order. For example, if you do:
252  // MapPath("bar", "foo/bar");
253  // MapPath("", "baz");
254  // and then you do:
255  // Open("bar/qux");
256  // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux,
257  // returning the first one that opens successfuly.
258  //
259  // disk_path may be an absolute path or relative to the current directory,
260  // just like a path you'd pass to open().
261  void MapPath(const std::string& virtual_path, const std::string& disk_path);
262 
263  // Return type for DiskFileToVirtualFile().
268  NO_MAPPING
269  };
270 
271  // Given a path to a file on disk, find a virtual path mapping to that
272  // file. The first mapping created with MapPath() whose disk_path contains
273  // the filename is used. However, that virtual path may not actually be
274  // usable to open the given file. Possible return values are:
275  // * SUCCESS: The mapping was found. *virtual_file is filled in so that
276  // calling Open(*virtual_file) will open the file named by disk_file.
277  // * SHADOWED: A mapping was found, but using Open() to open this virtual
278  // path will end up returning some different file. This is because some
279  // other mapping with a higher precedence also matches this virtual path
280  // and maps it to a different file that exists on disk. *virtual_file
281  // is filled in as it would be in the SUCCESS case. *shadowing_disk_file
282  // is filled in with the disk path of the file which would be opened if
283  // you were to call Open(*virtual_file).
284  // * CANNOT_OPEN: The mapping was found and was not shadowed, but the
285  // file specified cannot be opened. When this value is returned,
286  // errno will indicate the reason the file cannot be opened. *virtual_file
287  // will be set to the virtual path as in the SUCCESS case, even though
288  // it is not useful.
289  // * NO_MAPPING: Indicates that no mapping was found which contains this
290  // file.
291  DiskFileToVirtualFileResult DiskFileToVirtualFile(
292  const std::string& disk_file, std::string* virtual_file,
293  std::string* shadowing_disk_file);
294 
295  // Given a virtual path, find the path to the file on disk.
296  // Return true and update disk_file with the on-disk path if the file exists.
297  // Return false and leave disk_file untouched if the file doesn't exist.
298  bool VirtualFileToDiskFile(const std::string& virtual_file,
299  std::string* disk_file);
300 
301  // implements SourceTree -------------------------------------------
302  io::ZeroCopyInputStream* Open(const std::string& filename) override;
303 
304  std::string GetLastErrorMessage() override;
305 
306  private:
307  struct Mapping {
310 
311  inline Mapping(const std::string& virtual_path_param,
312  const std::string& disk_path_param)
313  : virtual_path(virtual_path_param), disk_path(disk_path_param) {}
314  };
315  std::vector<Mapping> mappings_;
317 
318  // Like Open(), but returns the on-disk path in disk_file if disk_file is
319  // non-NULL and the file could be successfully opened.
320  io::ZeroCopyInputStream* OpenVirtualFile(const std::string& virtual_file,
321  std::string* disk_file);
322 
323  // Like Open() but given the actual on-disk path.
324  io::ZeroCopyInputStream* OpenDiskFile(const std::string& filename);
325 
327 };
328 
329 } // namespace compiler
330 } // namespace protobuf
331 } // namespace google
332 
333 #include <google/protobuf/port_undef.inc>
334 
335 #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
google::protobuf::compiler::SourceTree::SourceTree
SourceTree()
Definition: importer.h:217
google::protobuf::compiler::SourceTreeDescriptorDatabase::source_locations_
SourceLocationTable source_locations_
Definition: importer.h:145
google::protobuf::compiler::Importer::database_
SourceTreeDescriptorDatabase database_
Definition: importer.h:186
google::protobuf::compiler::MultiFileErrorCollector::AddWarning
virtual void AddWarning(const std::string &filename, int line, int column, const std::string &message)
Definition: importer.h:204
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::python::cdescriptor_pool::FindFileByName
static PyObject * FindFileByName(PyObject *self, PyObject *arg)
Definition: descriptor_pool.cc:258
google::protobuf::compiler::DiskSourceTree::mappings_
std::vector< Mapping > mappings_
Definition: importer.h:315
google::protobuf::compiler::DiskSourceTree
Definition: importer.h:240
google::protobuf::compiler::DiskSourceTree::SHADOWED
@ SHADOWED
Definition: importer.h:266
google::protobuf::compiler::SourceTreeDescriptorDatabase
Definition: importer.h:80
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
descriptor_database.h
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::DiskSourceTree::last_error_message_
std::string last_error_message_
Definition: importer.h:316
google::protobuf::python::cdescriptor_pool::FindFileContainingSymbol
static PyObject * FindFileContainingSymbol(PyObject *self, PyObject *arg)
Definition: descriptor_pool.cc:400
google::protobuf::compiler::Importer
Definition: importer.h:157
google::protobuf::compiler::DiskSourceTree::DiskFileToVirtualFileResult
DiskFileToVirtualFileResult
Definition: importer.h:264
google::protobuf::compiler::MultiFileErrorCollector::MultiFileErrorCollector
MultiFileErrorCollector()
Definition: importer.h:196
google::protobuf::compiler::DiskSourceTree::Mapping::Mapping
Mapping(const std::string &virtual_path_param, const std::string &disk_path_param)
Definition: importer.h:311
google::protobuf::compiler::SourceTreeDescriptorDatabase::validation_error_collector_
ValidationErrorCollector validation_error_collector_
Definition: importer.h:146
google::protobuf::compiler::DiskSourceTree::Mapping
Definition: importer.h:307
google::protobuf::compiler::DiskSourceTree::CANNOT_OPEN
@ CANNOT_OPEN
Definition: importer.h:267
google::protobuf::compiler::SourceTreeDescriptorDatabase::SingleFileErrorCollector
Definition: importer.cc:91
google::protobuf::DescriptorPool::ErrorCollector::ErrorLocation
ErrorLocation
Definition: src/google/protobuf/descriptor.h:1638
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
google::protobuf::compiler::SourceTreeDescriptorDatabase::ValidationErrorCollector
Definition: importer.h:123
google::protobuf::compiler::SourceTreeDescriptorDatabase::GetValidationErrorCollector
DescriptorPool::ErrorCollector * GetValidationErrorCollector()
Definition: importer.h:102
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::DescriptorDatabase
Definition: src/google/protobuf/descriptor_database.h:71
location
GLint location
Definition: glcorearb.h:3074
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::compiler::Importer::pool_
DescriptorPool pool_
Definition: importer.h:187
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf::compiler::Importer::pool
const DescriptorPool * pool() const
Definition: importer.h:179
error_collector_
MockErrorCollector error_collector_
Definition: importer_unittest.cc:129
google::protobuf::compiler::DiskSourceTree::Mapping::disk_path
std::string disk_path
Definition: importer.h:309
google::protobuf::compiler::SourceTreeDescriptorDatabase::using_validation_error_collector_
bool using_validation_error_collector_
Definition: importer.h:144
parser.h
google::protobuf::compiler::SourceTreeDescriptorDatabase::fallback_database_
DescriptorDatabase * fallback_database_
Definition: importer.h:120
descriptor.h
google::protobuf::compiler::SourceLocationTable
Definition: parser.h:557
element_name
std::string element_name
Definition: src/google/protobuf/descriptor.cc:3116
pool_
DescriptorPool pool_
Definition: parser_unittest.cc:183
google::protobuf::compiler::SourceTreeDescriptorDatabase::RecordErrorsTo
void RecordErrorsTo(MultiFileErrorCollector *error_collector)
Definition: importer.h:94
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::MultiFileErrorCollector
Definition: importer.h:194
google::protobuf::compiler::SourceTreeDescriptorDatabase::source_tree_
SourceTree * source_tree_
Definition: importer.h:117
google::protobuf::compiler::SourceTree
Definition: importer.h:215
google::protobuf::compiler::SourceTreeDescriptorDatabase::ValidationErrorCollector::owner_
SourceTreeDescriptorDatabase * owner_
Definition: importer.h:140
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::compiler::DiskSourceTree::SUCCESS
@ SUCCESS
Definition: importer.h:265
google::protobuf::compiler::DiskSourceTree::Mapping::virtual_path
std::string virtual_path
Definition: importer.h:308
google::protobuf::compiler::SourceTreeDescriptorDatabase::error_collector_
MultiFileErrorCollector * error_collector_
Definition: importer.h:121
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54