cppPreprocessor.h 6.9 KB

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