cppPreprocessor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Filename: cppPreprocessor.h
  2. // Created by: drose (22Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef CPPPREPROCESSOR_H
  15. #define CPPPREPROCESSOR_H
  16. #include "dtoolbase.h"
  17. #include "cppManifest.h"
  18. #include "cppToken.h"
  19. #include "cppFile.h"
  20. #include "cppCommentBlock.h"
  21. #include "dSearchPath.h"
  22. #include "vector_string.h"
  23. #include <map>
  24. #include <list>
  25. #include <vector>
  26. class CPPScope;
  27. class CPPTemplateParameterList;
  28. class CPPExpression;
  29. //#define CPP_VERBOSE_LEX
  30. ///////////////////////////////////////////////////////////////////
  31. // Class : CPPPreprocessor
  32. // Description :
  33. ////////////////////////////////////////////////////////////////////
  34. class CPPPreprocessor {
  35. public:
  36. CPPPreprocessor();
  37. void set_verbose(int verbose);
  38. int get_verbose() const;
  39. void copy_filepos(const CPPPreprocessor &other);
  40. CPPFile get_file() const;
  41. int get_line_number() const;
  42. int get_col_number() const;
  43. CPPToken get_next_token();
  44. #ifdef CPP_VERBOSE_LEX
  45. CPPToken get_next_token0();
  46. int _token_index;
  47. #endif
  48. void warning(const string &message, int line = 0, int col = 0,
  49. CPPFile file = CPPFile());
  50. void error(const string &message, int line = 0, int col = 0,
  51. CPPFile file = CPPFile());
  52. CPPCommentBlock *get_comment_before(int line, CPPFile file);
  53. CPPCommentBlock *get_comment_on(int line, CPPFile file);
  54. int get_warning_count() const;
  55. int get_error_count() const;
  56. typedef map<string, CPPManifest *> Manifests;
  57. Manifests _manifests;
  58. pvector<CPPFile::Source> _quote_include_kind;
  59. DSearchPath _quote_include_path;
  60. DSearchPath _angle_include_path;
  61. bool _noangles;
  62. CPPComments _comments;
  63. typedef set<CPPFile> ParsedFiles;
  64. ParsedFiles _parsed_files;
  65. typedef set<string> Includes;
  66. Includes _quote_includes;
  67. Includes _angle_includes;
  68. // This is normally true, to indicate that the preprocessor should
  69. // decode identifiers like foo::bar<snarf> into a single IDENTIFIER,
  70. // TYPENAME_IDENTIFIER, or SCOPING token for yacc's convenience.
  71. // When false, it leaves them alone and returns a sequence of
  72. // SIMPLE_IDENTIFIER and SCOPE tokens instead.
  73. bool _resolve_identifiers;
  74. // The default _verbose level is 1, which will output normal error
  75. // and warning messages but nothing else. Set this to 0 to make the
  76. // warning messages go away (although the counts will still be
  77. // incremented), or set it higher to get more debugging information.
  78. int _verbose;
  79. protected:
  80. bool init_cpp(const CPPFile &file);
  81. bool init_const_expr(const string &expr);
  82. bool init_type(const string &type);
  83. bool push_file(const CPPFile &file);
  84. bool push_string(const string &input, bool lock_position);
  85. string expand_manifests(const string &input_expr);
  86. CPPExpression *parse_expr(const string &expr, CPPScope *current_scope,
  87. CPPScope *global_scope);
  88. private:
  89. CPPToken internal_get_next_token();
  90. int skip_whitespace(int c);
  91. int skip_comment(int c);
  92. int skip_c_comment(int c);
  93. int skip_cpp_comment(int c);
  94. int process_directive(int c);
  95. int get_preprocessor_command(int c, string &command);
  96. int get_preprocessor_args(int c, string &args);
  97. void handle_define_directive(const string &args, int first_line,
  98. int first_col, const CPPFile &first_file);
  99. void handle_undef_directive(const string &args, int first_line,
  100. int first_col, const CPPFile &first_file);
  101. void handle_ifdef_directive(const string &args, int first_line,
  102. int first_col, const CPPFile &first_file);
  103. void handle_ifndef_directive(const string &args, int first_line,
  104. int first_col, const CPPFile &first_file);
  105. void handle_if_directive(const string &args, int first_line,
  106. int first_col, const CPPFile &first_file);
  107. void handle_include_directive(const string &args, int first_line,
  108. int first_col, const CPPFile &first_file);
  109. void handle_pragma_directive(const string &args, int first_line,
  110. int first_col, const CPPFile &first_file);
  111. void handle_error_directive(const string &args, int first_line,
  112. int first_col, const CPPFile &first_file);
  113. void skip_false_if_block(bool consider_elifs);
  114. CPPToken get_quoted_char(int c);
  115. CPPToken get_quoted_string(int c);
  116. CPPToken get_identifier(int c);
  117. CPPToken expand_manifest(const CPPManifest *manifest);
  118. void extract_manifest_args(const string &name, int num_args,
  119. int va_arg, vector_string &args);
  120. void expand_defined_function(string &expr, size_t q, size_t &p);
  121. void expand_manifest_inline(string &expr, size_t q, size_t &p,
  122. const CPPManifest *manifest);
  123. void extract_manifest_args_inline(const string &name, int num_args,
  124. int va_arg, vector_string &args,
  125. const string &expr, size_t &p);
  126. CPPToken get_number(int c, int c2 = 0);
  127. static int check_keyword(const string &name);
  128. int scan_escape_sequence(int c);
  129. string scan_quoted(int c);
  130. bool should_ignore_manifest(const CPPManifest *manifest) const;
  131. bool should_ignore_preprocessor() const;
  132. int get();
  133. void unget(int c);
  134. CPPTemplateParameterList *
  135. nested_parse_template_instantiation(CPPTemplateScope *scope);
  136. void skip_to_end_nested();
  137. void skip_to_angle_bracket();
  138. class InputFile {
  139. public:
  140. InputFile();
  141. ~InputFile();
  142. bool open(const CPPFile &file);
  143. bool connect_input(const string &input);
  144. int get();
  145. const CPPManifest *_ignore_manifest;
  146. CPPFile _file;
  147. string _input;
  148. istream *_in;
  149. int _line_number;
  150. int _col_number;
  151. bool _lock_position;
  152. int _prev_last_c;
  153. };
  154. // This must be a list and not a vector because we don't have a good
  155. // copy constructor defined for InputFile.
  156. typedef list<InputFile> Files;
  157. Files _files;
  158. enum State {
  159. S_normal, S_eof, S_nested, S_end_nested
  160. };
  161. State _state;
  162. int _paren_nesting;
  163. bool _angle_bracket_found;
  164. bool _start_of_line;
  165. int _unget;
  166. int _last_c;
  167. bool _last_cpp_comment;
  168. bool _save_comments;
  169. vector<CPPToken> _saved_tokens;
  170. int _warning_count;
  171. int _error_count;
  172. bool _error_abort;
  173. };
  174. #endif