You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

importer.h 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file is the public interface to the .proto file parser.
  35. #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
  36. #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
  37. #include <set>
  38. #include <string>
  39. #include <utility>
  40. #include <vector>
  41. #include <google/protobuf/compiler/parser.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/descriptor_database.h>
  44. // Must be included last.
  45. #include <google/protobuf/port_def.inc>
  46. namespace google
  47. {
  48. namespace protobuf
  49. {
  50. namespace io
  51. {
  52. class ZeroCopyInputStream;
  53. }
  54. namespace compiler
  55. {
  56. // Defined in this file.
  57. class Importer;
  58. class MultiFileErrorCollector;
  59. class SourceTree;
  60. class DiskSourceTree;
  61. // TODO(kenton): Move all SourceTree stuff to a separate file?
  62. // An implementation of DescriptorDatabase which loads files from a SourceTree
  63. // and parses them.
  64. //
  65. // Note: This class is not thread-safe since it maintains a table of source
  66. // code locations for error reporting. However, when a DescriptorPool wraps
  67. // a DescriptorDatabase, it uses mutex locking to make sure only one method
  68. // of the database is called at a time, even if the DescriptorPool is used
  69. // from multiple threads. Therefore, there is only a problem if you create
  70. // multiple DescriptorPools wrapping the same SourceTreeDescriptorDatabase
  71. // and use them from multiple threads.
  72. //
  73. // Note: This class does not implement FindFileContainingSymbol() or
  74. // FindFileContainingExtension(); these will always return false.
  75. class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase
  76. {
  77. public:
  78. SourceTreeDescriptorDatabase(SourceTree* source_tree);
  79. // If non-NULL, fallback_database will be checked if a file doesn't exist in
  80. // the specified source_tree.
  81. SourceTreeDescriptorDatabase(SourceTree* source_tree, DescriptorDatabase* fallback_database);
  82. ~SourceTreeDescriptorDatabase() override;
  83. // Instructs the SourceTreeDescriptorDatabase to report any parse errors
  84. // to the given MultiFileErrorCollector. This should be called before
  85. // parsing. error_collector must remain valid until either this method
  86. // is called again or the SourceTreeDescriptorDatabase is destroyed.
  87. void RecordErrorsTo(MultiFileErrorCollector* error_collector)
  88. {
  89. error_collector_ = error_collector;
  90. }
  91. // Gets a DescriptorPool::ErrorCollector which records errors to the
  92. // MultiFileErrorCollector specified with RecordErrorsTo(). This collector
  93. // has the ability to determine exact line and column numbers of errors
  94. // from the information given to it by the DescriptorPool.
  95. DescriptorPool::ErrorCollector* GetValidationErrorCollector()
  96. {
  97. using_validation_error_collector_ = true;
  98. return &validation_error_collector_;
  99. }
  100. // implements DescriptorDatabase -----------------------------------
  101. bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override;
  102. bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override;
  103. bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override;
  104. private:
  105. class SingleFileErrorCollector;
  106. SourceTree* source_tree_;
  107. DescriptorDatabase* fallback_database_;
  108. MultiFileErrorCollector* error_collector_;
  109. class PROTOBUF_EXPORT ValidationErrorCollector : public DescriptorPool::ErrorCollector
  110. {
  111. public:
  112. ValidationErrorCollector(SourceTreeDescriptorDatabase* owner);
  113. ~ValidationErrorCollector() override;
  114. // implements ErrorCollector ---------------------------------------
  115. void AddError(const std::string& filename, const std::string& element_name, const Message* descriptor, ErrorLocation location, const std::string& message) override;
  116. void AddWarning(const std::string& filename, const std::string& element_name, const Message* descriptor, ErrorLocation location, const std::string& message) override;
  117. private:
  118. SourceTreeDescriptorDatabase* owner_;
  119. };
  120. friend class ValidationErrorCollector;
  121. bool using_validation_error_collector_;
  122. SourceLocationTable source_locations_;
  123. ValidationErrorCollector validation_error_collector_;
  124. };
  125. // Simple interface for parsing .proto files. This wraps the process
  126. // of opening the file, parsing it with a Parser, recursively parsing all its
  127. // imports, and then cross-linking the results to produce a FileDescriptor.
  128. //
  129. // This is really just a thin wrapper around SourceTreeDescriptorDatabase.
  130. // You may find that SourceTreeDescriptorDatabase is more flexible.
  131. //
  132. // TODO(kenton): I feel like this class is not well-named.
  133. class PROTOBUF_EXPORT Importer
  134. {
  135. public:
  136. Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector);
  137. ~Importer();
  138. // Import the given file and build a FileDescriptor representing it. If
  139. // the file is already in the DescriptorPool, the existing FileDescriptor
  140. // will be returned. The FileDescriptor is property of the DescriptorPool,
  141. // and will remain valid until it is destroyed. If any errors occur, they
  142. // will be reported using the error collector and Import() will return NULL.
  143. //
  144. // A particular Importer object will only report errors for a particular
  145. // file once. All future attempts to import the same file will return NULL
  146. // without reporting any errors. The idea is that you might want to import
  147. // a lot of files without seeing the same errors over and over again. If
  148. // you want to see errors for the same files repeatedly, you can use a
  149. // separate Importer object to import each one (but use the same
  150. // DescriptorPool so that they can be cross-linked).
  151. const FileDescriptor* Import(const std::string& filename);
  152. // The DescriptorPool in which all imported FileDescriptors and their
  153. // contents are stored.
  154. inline const DescriptorPool* pool() const
  155. {
  156. return &pool_;
  157. }
  158. void AddUnusedImportTrackFile(const std::string& file_name, bool is_error = false);
  159. void ClearUnusedImportTrackFiles();
  160. private:
  161. SourceTreeDescriptorDatabase database_;
  162. DescriptorPool pool_;
  163. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Importer);
  164. };
  165. // If the importer encounters problems while trying to import the proto files,
  166. // it reports them to a MultiFileErrorCollector.
  167. class PROTOBUF_EXPORT MultiFileErrorCollector
  168. {
  169. public:
  170. inline MultiFileErrorCollector()
  171. {
  172. }
  173. virtual ~MultiFileErrorCollector();
  174. // Line and column numbers are zero-based. A line number of -1 indicates
  175. // an error with the entire file (e.g. "not found").
  176. virtual void AddError(const std::string& filename, int line, int column, const std::string& message) = 0;
  177. virtual void AddWarning(const std::string& /* filename */, int /* line */, int /* column */, const std::string& /* message */)
  178. {
  179. }
  180. private:
  181. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector);
  182. };
  183. // Abstract interface which represents a directory tree containing proto files.
  184. // Used by the default implementation of Importer to resolve import statements
  185. // Most users will probably want to use the DiskSourceTree implementation,
  186. // below.
  187. class PROTOBUF_EXPORT SourceTree
  188. {
  189. public:
  190. inline SourceTree()
  191. {
  192. }
  193. virtual ~SourceTree();
  194. // Open the given file and return a stream that reads it, or NULL if not
  195. // found. The caller takes ownership of the returned object. The filename
  196. // must be a path relative to the root of the source tree and must not
  197. // contain "." or ".." components.
  198. virtual io::ZeroCopyInputStream* Open(const std::string& filename) = 0;
  199. // If Open() returns NULL, calling this method immediately will return an
  200. // description of the error.
  201. // Subclasses should implement this method and return a meaningful value for
  202. // better error reporting.
  203. // TODO(xiaofeng): change this to a pure virtual function.
  204. virtual std::string GetLastErrorMessage();
  205. private:
  206. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree);
  207. };
  208. // An implementation of SourceTree which loads files from locations on disk.
  209. // Multiple mappings can be set up to map locations in the DiskSourceTree to
  210. // locations in the physical filesystem.
  211. class PROTOBUF_EXPORT DiskSourceTree : public SourceTree
  212. {
  213. public:
  214. DiskSourceTree();
  215. ~DiskSourceTree() override;
  216. // Map a path on disk to a location in the SourceTree. The path may be
  217. // either a file or a directory. If it is a directory, the entire tree
  218. // under it will be mapped to the given virtual location. To map a directory
  219. // to the root of the source tree, pass an empty string for virtual_path.
  220. //
  221. // If multiple mapped paths apply when opening a file, they will be searched
  222. // in order. For example, if you do:
  223. // MapPath("bar", "foo/bar");
  224. // MapPath("", "baz");
  225. // and then you do:
  226. // Open("bar/qux");
  227. // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux,
  228. // returning the first one that opens successfully.
  229. //
  230. // disk_path may be an absolute path or relative to the current directory,
  231. // just like a path you'd pass to open().
  232. void MapPath(const std::string& virtual_path, const std::string& disk_path);
  233. // Return type for DiskFileToVirtualFile().
  234. enum DiskFileToVirtualFileResult
  235. {
  236. SUCCESS,
  237. SHADOWED,
  238. CANNOT_OPEN,
  239. NO_MAPPING
  240. };
  241. // Given a path to a file on disk, find a virtual path mapping to that
  242. // file. The first mapping created with MapPath() whose disk_path contains
  243. // the filename is used. However, that virtual path may not actually be
  244. // usable to open the given file. Possible return values are:
  245. // * SUCCESS: The mapping was found. *virtual_file is filled in so that
  246. // calling Open(*virtual_file) will open the file named by disk_file.
  247. // * SHADOWED: A mapping was found, but using Open() to open this virtual
  248. // path will end up returning some different file. This is because some
  249. // other mapping with a higher precedence also matches this virtual path
  250. // and maps it to a different file that exists on disk. *virtual_file
  251. // is filled in as it would be in the SUCCESS case. *shadowing_disk_file
  252. // is filled in with the disk path of the file which would be opened if
  253. // you were to call Open(*virtual_file).
  254. // * CANNOT_OPEN: The mapping was found and was not shadowed, but the
  255. // file specified cannot be opened. When this value is returned,
  256. // errno will indicate the reason the file cannot be opened. *virtual_file
  257. // will be set to the virtual path as in the SUCCESS case, even though
  258. // it is not useful.
  259. // * NO_MAPPING: Indicates that no mapping was found which contains this
  260. // file.
  261. DiskFileToVirtualFileResult DiskFileToVirtualFile(
  262. const std::string& disk_file, std::string* virtual_file, std::string* shadowing_disk_file
  263. );
  264. // Given a virtual path, find the path to the file on disk.
  265. // Return true and update disk_file with the on-disk path if the file exists.
  266. // Return false and leave disk_file untouched if the file doesn't exist.
  267. bool VirtualFileToDiskFile(const std::string& virtual_file, std::string* disk_file);
  268. // implements SourceTree -------------------------------------------
  269. io::ZeroCopyInputStream* Open(const std::string& filename) override;
  270. std::string GetLastErrorMessage() override;
  271. private:
  272. struct Mapping
  273. {
  274. std::string virtual_path;
  275. std::string disk_path;
  276. inline Mapping(const std::string& virtual_path_param, const std::string& disk_path_param) :
  277. virtual_path(virtual_path_param),
  278. disk_path(disk_path_param)
  279. {
  280. }
  281. };
  282. std::vector<Mapping> mappings_;
  283. std::string last_error_message_;
  284. // Like Open(), but returns the on-disk path in disk_file if disk_file is
  285. // non-NULL and the file could be successfully opened.
  286. io::ZeroCopyInputStream* OpenVirtualFile(const std::string& virtual_file, std::string* disk_file);
  287. // Like Open() but given the actual on-disk path.
  288. io::ZeroCopyInputStream* OpenDiskFile(const std::string& filename);
  289. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree);
  290. };
  291. } // namespace compiler
  292. } // namespace protobuf
  293. } // namespace google
  294. #include <google/protobuf/port_undef.inc>
  295. #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__