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