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 void ClearUnusedImportTrackFiles(); 183 184 185 private: 186 SourceTreeDescriptorDatabase database_; 187 DescriptorPool pool_; 188 189 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Importer); 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: MultiFileErrorCollector()196 inline MultiFileErrorCollector() {} 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 AddWarning(const std::string & filename,int line,int column,const std::string & message)204 virtual void AddWarning(const std::string& filename, int line, int column, 205 const std::string& message) {} 206 207 private: 208 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector); 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: SourceTree()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: 234 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree); 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(). 264 enum DiskFileToVirtualFileResult { 265 SUCCESS, 266 SHADOWED, 267 CANNOT_OPEN, 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 { 308 std::string virtual_path; 309 std::string disk_path; 310 MappingMapping311 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_; 316 std::string last_error_message_; 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 326 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree); 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__ 336