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> 44 #include <google/protobuf/compiler/parser.h> 45 #include <google/protobuf/descriptor.h> 46 #include <google/protobuf/descriptor_database.h> 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: 82 SourceTreeDescriptorDatabase(SourceTree* source_tree); 83 84 // If non-NULL, fallback_database will be checked if a file doesn't exist in 85 // the specified source_tree. 86 SourceTreeDescriptorDatabase(SourceTree* source_tree, 87 DescriptorDatabase* fallback_database); 88 ~SourceTreeDescriptorDatabase(); 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. RecordErrorsTo(MultiFileErrorCollector * error_collector)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. GetValidationErrorCollector()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: 117 class SingleFileErrorCollector; 118 119 SourceTree* source_tree_; 120 DescriptorDatabase* fallback_database_; 121 MultiFileErrorCollector* error_collector_; 122 123 class PROTOBUF_EXPORT ValidationErrorCollector 124 : public DescriptorPool::ErrorCollector { 125 public: 126 ValidationErrorCollector(SourceTreeDescriptorDatabase* owner); 127 ~ValidationErrorCollector(); 128 129 // implements ErrorCollector --------------------------------------- 130 void AddError(const std::string& filename, const std::string& element_name, 131 const Message* descriptor, ErrorLocation location, 132 const std::string& message) override; 133 134 void AddWarning(const std::string& filename, 135 const std::string& element_name, const Message* descriptor, 136 ErrorLocation location, 137 const std::string& message) override; 138 139 private: 140 SourceTreeDescriptorDatabase* owner_; 141 }; 142 friend class ValidationErrorCollector; 143 144 bool using_validation_error_collector_; 145 SourceLocationTable source_locations_; 146 ValidationErrorCollector validation_error_collector_; 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. pool()179 inline const DescriptorPool* pool() const { return &pool_; } 180 181 void AddUnusedImportTrackFile(const std::string& file_name, 182 bool is_error = false); 183 void ClearUnusedImportTrackFiles(); 184 185 186 private: 187 SourceTreeDescriptorDatabase database_; 188 DescriptorPool pool_; 189 190 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Importer); 191 }; 192 193 // If the importer encounters problems while trying to import the proto files, 194 // it reports them to a MultiFileErrorCollector. 195 class PROTOBUF_EXPORT MultiFileErrorCollector { 196 public: MultiFileErrorCollector()197 inline MultiFileErrorCollector() {} 198 virtual ~MultiFileErrorCollector(); 199 200 // Line and column numbers are zero-based. A line number of -1 indicates 201 // an error with the entire file (e.g. "not found"). 202 virtual void AddError(const std::string& filename, int line, int column, 203 const std::string& message) = 0; 204 AddWarning(const std::string & filename,int line,int column,const std::string & message)205 virtual void AddWarning(const std::string& filename, int line, int column, 206 const std::string& message) {} 207 208 private: 209 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector); 210 }; 211 212 // Abstract interface which represents a directory tree containing proto files. 213 // Used by the default implementation of Importer to resolve import statements 214 // Most users will probably want to use the DiskSourceTree implementation, 215 // below. 216 class PROTOBUF_EXPORT SourceTree { 217 public: SourceTree()218 inline SourceTree() {} 219 virtual ~SourceTree(); 220 221 // Open the given file and return a stream that reads it, or NULL if not 222 // found. The caller takes ownership of the returned object. The filename 223 // must be a path relative to the root of the source tree and must not 224 // contain "." or ".." components. 225 virtual io::ZeroCopyInputStream* Open(const std::string& filename) = 0; 226 227 // If Open() returns NULL, calling this method immediately will return an 228 // description of the error. 229 // Subclasses should implement this method and return a meaningful value for 230 // better error reporting. 231 // TODO(xiaofeng): change this to a pure virtual function. 232 virtual std::string GetLastErrorMessage(); 233 234 private: 235 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree); 236 }; 237 238 // An implementation of SourceTree which loads files from locations on disk. 239 // Multiple mappings can be set up to map locations in the DiskSourceTree to 240 // locations in the physical filesystem. 241 class PROTOBUF_EXPORT DiskSourceTree : public SourceTree { 242 public: 243 DiskSourceTree(); 244 ~DiskSourceTree(); 245 246 // Map a path on disk to a location in the SourceTree. The path may be 247 // either a file or a directory. If it is a directory, the entire tree 248 // under it will be mapped to the given virtual location. To map a directory 249 // to the root of the source tree, pass an empty string for virtual_path. 250 // 251 // If multiple mapped paths apply when opening a file, they will be searched 252 // in order. For example, if you do: 253 // MapPath("bar", "foo/bar"); 254 // MapPath("", "baz"); 255 // and then you do: 256 // Open("bar/qux"); 257 // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux, 258 // returning the first one that opens successfully. 259 // 260 // disk_path may be an absolute path or relative to the current directory, 261 // just like a path you'd pass to open(). 262 void MapPath(const std::string& virtual_path, const std::string& disk_path); 263 264 // Return type for DiskFileToVirtualFile(). 265 enum DiskFileToVirtualFileResult { 266 SUCCESS, 267 SHADOWED, 268 CANNOT_OPEN, 269 NO_MAPPING 270 }; 271 272 // Given a path to a file on disk, find a virtual path mapping to that 273 // file. The first mapping created with MapPath() whose disk_path contains 274 // the filename is used. However, that virtual path may not actually be 275 // usable to open the given file. Possible return values are: 276 // * SUCCESS: The mapping was found. *virtual_file is filled in so that 277 // calling Open(*virtual_file) will open the file named by disk_file. 278 // * SHADOWED: A mapping was found, but using Open() to open this virtual 279 // path will end up returning some different file. This is because some 280 // other mapping with a higher precedence also matches this virtual path 281 // and maps it to a different file that exists on disk. *virtual_file 282 // is filled in as it would be in the SUCCESS case. *shadowing_disk_file 283 // is filled in with the disk path of the file which would be opened if 284 // you were to call Open(*virtual_file). 285 // * CANNOT_OPEN: The mapping was found and was not shadowed, but the 286 // file specified cannot be opened. When this value is returned, 287 // errno will indicate the reason the file cannot be opened. *virtual_file 288 // will be set to the virtual path as in the SUCCESS case, even though 289 // it is not useful. 290 // * NO_MAPPING: Indicates that no mapping was found which contains this 291 // file. 292 DiskFileToVirtualFileResult DiskFileToVirtualFile( 293 const std::string& disk_file, std::string* virtual_file, 294 std::string* shadowing_disk_file); 295 296 // Given a virtual path, find the path to the file on disk. 297 // Return true and update disk_file with the on-disk path if the file exists. 298 // Return false and leave disk_file untouched if the file doesn't exist. 299 bool VirtualFileToDiskFile(const std::string& virtual_file, 300 std::string* disk_file); 301 302 // implements SourceTree ------------------------------------------- 303 io::ZeroCopyInputStream* Open(const std::string& filename) override; 304 305 std::string GetLastErrorMessage() override; 306 307 private: 308 struct Mapping { 309 std::string virtual_path; 310 std::string disk_path; 311 MappingMapping312 inline Mapping(const std::string& virtual_path_param, 313 const std::string& disk_path_param) 314 : virtual_path(virtual_path_param), disk_path(disk_path_param) {} 315 }; 316 std::vector<Mapping> mappings_; 317 std::string last_error_message_; 318 319 // Like Open(), but returns the on-disk path in disk_file if disk_file is 320 // non-NULL and the file could be successfully opened. 321 io::ZeroCopyInputStream* OpenVirtualFile(const std::string& virtual_file, 322 std::string* disk_file); 323 324 // Like Open() but given the actual on-disk path. 325 io::ZeroCopyInputStream* OpenDiskFile(const std::string& filename); 326 327 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree); 328 }; 329 330 } // namespace compiler 331 } // namespace protobuf 332 } // namespace google 333 334 #include <google/protobuf/port_undef.inc> 335 336 #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ 337