1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_C_INCLUDE_ITERATOR_H_ 6 #define TOOLS_GN_C_INCLUDE_ITERATOR_H_ 7 8 #include <stddef.h> 9 10 #include <string_view> 11 12 #include "gn/location.h" 13 14 class InputFile; 15 16 struct IncludeStringWithLocation { 17 std::string_view contents; 18 LocationRange location; 19 bool system_style_include = false; 20 }; 21 22 // Iterates through #includes in C source and header files. 23 class CIncludeIterator { 24 public: 25 // The InputFile pointed to must outlive this class. 26 explicit CIncludeIterator(const InputFile* input); 27 ~CIncludeIterator(); 28 29 // Fills in the string with the contents of the next include, and the 30 // location with where it came from, and returns true, or returns false if 31 // there are no more includes. 32 bool GetNextIncludeString(IncludeStringWithLocation* include); 33 34 // Maximum numbef of non-includes we'll tolerate before giving up. This does 35 // not count comments or preprocessor. 36 static const int kMaxNonIncludeLines; 37 38 private: 39 // Returns false on EOF, otherwise fills in the given line and the one-based 40 // line number into *line_number; 41 bool GetNextLine(std::string_view* line, int* line_number); 42 43 const InputFile* input_file_; 44 45 // This just points into input_file_.contents() for convenience. 46 std::string_view file_; 47 48 // 0-based offset into the file. 49 size_t offset_ = 0; 50 51 int line_number_ = 0; // One-based. Indicates the last line we read. 52 53 // Number of lines we've processed since seeing the last include (or the 54 // beginning of the file) with some exceptions. 55 int lines_since_last_include_ = 0; 56 57 CIncludeIterator(const CIncludeIterator&) = delete; 58 CIncludeIterator& operator=(const CIncludeIterator&) = delete; 59 }; 60 61 #endif // TOOLS_GN_C_INCLUDE_ITERATOR_H_ 62