1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*=============================================================================
Boost.Wave: A Standard compliant C++ preprocessor library
http://www.boost.org/
State machine detecting include guards in an included file.
This detects two forms of include guards:
#ifndef INCLUDE_GUARD_MACRO
#define INCLUDE_GUARD_MACRO
...
#endif
or
if !defined(INCLUDE_GUARD_MACRO)
#define INCLUDE_GUARD_MACRO
...
#endif
note, that the parenthesis are optional (i.e. !defined INCLUDE_GUARD_MACRO
will work as well). The code allows for any whitespace, newline and single
'#' tokens before the #if/#ifndef and after the final #endif.
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(DETECT_INCLUDE_GUARDS_HK060304_INCLUDED)
#define DETECT_INCLUDE_GUARDS_HK060304_INCLUDED
#include <boost/wave/wave_config.hpp>
#include <boost/wave/token_ids.hpp>
// this must occur after all of the includes and before any code appears
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_PREFIX
#endif
///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace wave {
namespace cpplexer {
template <typename Token>
class include_guards
{
public:
include_guards()
: state(&include_guards::state_0), detected_guards(false),
current_state(true), if_depth(0)
{}
Token& detect_guard(Token& t)
{ return current_state ? (this->*state)(t) : t; }
bool detected(std::string& guard_name_) const
{
if (detected_guards) {
guard_name_ = guard_name.c_str();
return true;
}
return false;
}
private:
typedef Token& state_type(Token& t);
state_type include_guards::* state;
bool detected_guards;
bool current_state;
typename Token::string_type guard_name;
int if_depth;
state_type state_0, state_1, state_2, state_3, state_4, state_5;
state_type state_1a, state_1b, state_1c, state_1d, state_1e;
bool is_skippable(token_id id) const
{
return (T_POUND == BASE_TOKEN(id) ||
IS_CATEGORY(id, WhiteSpaceTokenType) ||
IS_CATEGORY(id, EOLTokenType));
}
};
///////////////////////////////////////////////////////////////////////////////
// state 0: beginning of a file, tries to recognize #ifndef or #if tokens
template <typename Token>
inline Token&
include_guards<Token>::state_0(Token& t)
{
token_id id = token_id(t);
if (T_PP_IFNDEF == id)
state = &include_guards::state_1;
else if (T_PP_IF == id)
state = &include_guards::state_1a;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1: found #ifndef, looking for T_IDENTIFIER
template <typename Token>
inline Token&
include_guards<Token>::state_1(Token& t)
{
token_id id = token_id(t);
if (T_IDENTIFIER == id) {
guard_name = t.get_value();
state = &include_guards::state_2;
}
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1a: found T_PP_IF, looking for T_NOT ("!")
template <typename Token>
inline Token&
include_guards<Token>::state_1a(Token& t)
{
token_id id = token_id(t);
if (T_NOT == BASE_TOKEN(id))
state = &include_guards::state_1b;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1b: found T_NOT, looking for 'defined'
template <typename Token>
inline Token&
include_guards<Token>::state_1b(Token& t)
{
token_id id = token_id(t);
if (T_IDENTIFIER == id && t.get_value() == "defined")
state = &include_guards::state_1c;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1c: found 'defined', looking for (optional) T_LEFTPAREN
template <typename Token>
inline Token&
include_guards<Token>::state_1c(Token& t)
{
token_id id = token_id(t);
if (T_LEFTPAREN == id)
state = &include_guards::state_1d;
else if (T_IDENTIFIER == id) {
guard_name = t.get_value();
state = &include_guards::state_2;
}
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1d: found T_LEFTPAREN, looking for T_IDENTIFIER guard
template <typename Token>
inline Token&
include_guards<Token>::state_1d(Token& t)
{
token_id id = token_id(t);
if (T_IDENTIFIER == id) {
guard_name = t.get_value();
state = &include_guards::state_1e;
}
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 1e: found T_IDENTIFIER guard, looking for T_RIGHTPAREN
template <typename Token>
inline Token&
include_guards<Token>::state_1e(Token& t)
{
token_id id = token_id(t);
if (T_RIGHTPAREN == id)
state = &include_guards::state_2;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 2: found T_IDENTIFIER, looking for #define
template <typename Token>
inline Token&
include_guards<Token>::state_2(Token& t)
{
token_id id = token_id(t);
if (T_PP_DEFINE == id)
state = &include_guards::state_3;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 3: found #define, looking for T_IDENTIFIER as recognized by state 1
template <typename Token>
inline Token&
include_guards<Token>::state_3(Token& t)
{
token_id id = token_id(t);
if (T_IDENTIFIER == id && t.get_value() == guard_name)
state = &include_guards::state_4;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 4: found guard T_IDENTIFIER, looking for #endif
template <typename Token>
inline Token&
include_guards<Token>::state_4(Token& t)
{
token_id id = token_id(t);
if (T_PP_IF == id || T_PP_IFDEF == id || T_PP_IFNDEF == id)
++if_depth;
else if (T_PP_ENDIF == id) {
if (if_depth > 0)
--if_depth;
else
state = &include_guards::state_5;
}
return t;
}
///////////////////////////////////////////////////////////////////////////////
// state 5: found final #endif, looking for T_EOF
template <typename Token>
inline Token&
include_guards<Token>::state_5(Token& t)
{
token_id id = token_id(t);
if (T_EOF == id)
detected_guards = current_state;
else if (!is_skippable(id))
current_state = false;
return t;
}
///////////////////////////////////////////////////////////////////////////////
} // namespace cpplexer
} // namespace wave
} // namespace boost
// the suffix header occurs after all of the code
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif
#endif // !DETECT_INCLUDE_GUARDS_HK060304_INCLUDED
|