|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | //--------------------------------------------------------------------------- |
| 20 | + |
20 | 21 | #include "tokenlist.h" |
21 | 22 |
|
22 | 23 | #include "astutils.h" |
|
40 | 41 |
|
41 | 42 | #include <simplecpp.h> |
42 | 43 |
|
| 44 | +//#define N_ASSERT_LANG |
| 45 | + |
| 46 | +#ifndef N_ASSERT_LANG |
| 47 | +#include <cassert> |
| 48 | +#define ASSERT_LANG(x) assert(x) |
| 49 | +#else |
| 50 | +#define ASSERT_LANG(x) |
| 51 | +#endif |
| 52 | + |
43 | 53 | // How many compileExpression recursions are allowed? |
44 | 54 | // For practical code this could be endless. But in some special torture test |
45 | 55 | // there needs to be a limit. |
@@ -83,7 +93,7 @@ void TokenList::deallocateTokens() |
83 | 93 |
|
84 | 94 | void TokenList::determineCppC() |
85 | 95 | { |
86 | | - // only try to determine it if it wasn't enforced |
| 96 | + // only try to determine if it wasn't enforced |
87 | 97 | if (mLang == Standards::Language::None) { |
88 | 98 | mLang = Path::identify(getSourceFilePath()); |
89 | 99 | } |
@@ -317,8 +327,31 @@ void TokenList::insertTokens(Token *dest, const Token *src, nonneg int n) |
317 | 327 |
|
318 | 328 | bool TokenList::createTokens(std::istream &code, const std::string& file0) |
319 | 329 | { |
| 330 | + ASSERT_LANG(!file0.empty()); |
| 331 | + |
320 | 332 | appendFileIfNew(file0); |
321 | 333 |
|
| 334 | + return createTokensInternal(code, file0); |
| 335 | +} |
| 336 | + |
| 337 | +//--------------------------------------------------------------------------- |
| 338 | + |
| 339 | +bool TokenList::createTokens(std::istream &code, Standards::Language lang) |
| 340 | +{ |
| 341 | + ASSERT_LANG(lang != Standards::Language::None); |
| 342 | + if (mLang == Standards::Language::None) { |
| 343 | + mLang = lang; |
| 344 | + } else { |
| 345 | + ASSERT_LANG(lang == mLang); |
| 346 | + } |
| 347 | + |
| 348 | + return createTokensInternal(code, ""); |
| 349 | +} |
| 350 | + |
| 351 | +//--------------------------------------------------------------------------- |
| 352 | + |
| 353 | +bool TokenList::createTokensInternal(std::istream &code, const std::string& file0) |
| 354 | +{ |
322 | 355 | simplecpp::OutputList outputList; |
323 | 356 | simplecpp::TokenList tokens(code, mFiles, file0, &outputList); |
324 | 357 |
|
@@ -2096,3 +2129,33 @@ bool TokenList::isKeyword(const std::string &str) const |
2096 | 2129 | static const auto& latest_c_keywords = Keywords::getAll(Standards::cstd_t::CLatest); |
2097 | 2130 | return latest_c_keywords.find(str) != latest_c_keywords.end(); |
2098 | 2131 | } |
| 2132 | + |
| 2133 | +bool TokenList::isC() const |
| 2134 | +{ |
| 2135 | + ASSERT_LANG(mLang != Standards::Language::None); |
| 2136 | + |
| 2137 | + // TODO: remove the fallback |
| 2138 | + if (mLang == Standards::Language::None) |
| 2139 | + return false; // treat as C++ by default |
| 2140 | + |
| 2141 | + return mLang == Standards::Language::C; |
| 2142 | +} |
| 2143 | + |
| 2144 | +bool TokenList::isCPP() const |
| 2145 | +{ |
| 2146 | + ASSERT_LANG(mLang != Standards::Language::None); |
| 2147 | + |
| 2148 | + // TODO: remove the fallback |
| 2149 | + if (mLang == Standards::Language::None) |
| 2150 | + return true; // treat as C++ by default |
| 2151 | + |
| 2152 | + return mLang == Standards::Language::CPP; |
| 2153 | +} |
| 2154 | + |
| 2155 | +void TokenList::setLang(Standards::Language lang) |
| 2156 | +{ |
| 2157 | + ASSERT_LANG(lang != Standards::Language::None); |
| 2158 | + ASSERT_LANG(mLang == Standards::Language::None); |
| 2159 | + |
| 2160 | + mLang = lang; |
| 2161 | +} |
0 commit comments