diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 759cffd03..c5ccdc73a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -60,20 +60,6 @@ const std::string& TokenList::getSourceFilePath() const return getFiles()[0]; } -bool TokenList::isC() const -{ - if (!_settings) - return Path::isC(getSourceFilePath()); - return _settings->enforcedLang == Settings::C || (_settings->enforcedLang == Settings::None && Path::isC(getSourceFilePath())); -} - -bool TokenList::isCPP() const -{ - if (!_settings) - return Path::isCPP(getSourceFilePath()); - return _settings->enforcedLang == Settings::CPP || (_settings->enforcedLang == Settings::None && Path::isCPP(getSourceFilePath())); -} - //--------------------------------------------------------------------------- // Deallocate lists.. @@ -94,6 +80,17 @@ unsigned int TokenList::appendFileIfNew(const std::string &fileName) // The "_files" vector remembers what files have been tokenized.. _files.push_back(Path::simplifyPath(fileName)); + + // Update _isC and _isCPP properties + if (_files.size() == 1) { // Update only useful if first file added to _files + if (!_settings) { + _isC = Path::isC(getSourceFilePath()); + _isCPP = Path::isCPP(getSourceFilePath()); + } else { + _isC = _settings->enforcedLang == Settings::C || (_settings->enforcedLang == Settings::None && Path::isC(getSourceFilePath())); + _isCPP = _settings->enforcedLang == Settings::CPP || (_settings->enforcedLang == Settings::None && Path::isCPP(getSourceFilePath())); + } + } return static_cast(_files.size() - 1); } @@ -210,7 +207,7 @@ void TokenList::insertTokens(Token *dest, const Token *src, unsigned int n) bool TokenList::createTokens(std::istream &code, const std::string& file0) { - _files.push_back(file0); + appendFileIfNew(file0); // line number in parsed code unsigned int lineno = 1; diff --git a/lib/tokenlist.h b/lib/tokenlist.h index 3134c7c8c..69f283240 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -44,10 +44,14 @@ public: const std::string& getSourceFilePath() const; /** Is the code C. Used for bailouts */ - bool isC() const; + bool isC() const { + return _isC; + } /** Is the code CPP. Used for bailouts */ - bool isCPP() const; + bool isCPP() const { + return _isCPP; + } /** * Delete all tokens in given token list @@ -136,6 +140,9 @@ private: /// private /** settings */ const Settings* _settings; + + /** File is known to be C/C++ code */ + bool _isC, _isCPP; }; /// @}