importer.h 14 KB

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