Skip to content

Commit c37a70e

Browse files
committed
store language in Preprocessor [skip ci]
1 parent 41b5054 commit c37a70e

7 files changed

+37
-30
lines changed

lib/cppcheck.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
910910
std::vector<std::string> files;
911911
simplecpp::TokenList tokens(*fileStream, files, file.spath());
912912
if (analyzerInformation) {
913-
const Preprocessor preprocessor(mSettings, mErrorLogger);
913+
const Preprocessor preprocessor(mSettings, mErrorLogger, Standards::Language::C);
914914
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
915915
}
916916
tokenizer.list.createTokens(std::move(tokens));
@@ -919,7 +919,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
919919
std::vector<std::string> files;
920920
simplecpp::TokenList tokens(file.spath(), files);
921921
if (analyzerInformation) {
922-
const Preprocessor preprocessor(mSettings, mErrorLogger);
922+
const Preprocessor preprocessor(mSettings, mErrorLogger, file.lang());
923923
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
924924
}
925925
tokenizer.list.createTokens(std::move(tokens));
@@ -964,9 +964,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
964964
return mLogger->exitcode();
965965
}
966966

967-
Preprocessor preprocessor(mSettings, mErrorLogger);
967+
Preprocessor preprocessor(mSettings, mErrorLogger, file.lang());
968968

969-
if (!preprocessor.loadFiles(tokens1, files, file.lang()))
969+
if (!preprocessor.loadFiles(tokens1, files))
970970
return mLogger->exitcode();
971971

972972
if (!mSettings.plistOutput.empty()) {
@@ -1032,7 +1032,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10321032

10331033
if (mSettings.checkConfiguration) {
10341034
for (const std::string &config : configurations)
1035-
(void)preprocessor.getcode(tokens1, config, files, file.lang(), true);
1035+
(void)preprocessor.getcode(tokens1, config, files, true);
10361036

10371037
if (analyzerInformation)
10381038
mLogger->setAnalyzerInfo(nullptr);
@@ -1105,7 +1105,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11051105
if (mSettings.preprocessOnly) {
11061106
std::string codeWithoutCfg;
11071107
Timer::run("Preprocessor::getcode", mSettings.showtime, &s_timerResults, [&]() {
1108-
codeWithoutCfg = preprocessor.getcode(tokens1, mCurrentConfig, files, file.lang(), true);
1108+
codeWithoutCfg = preprocessor.getcode(tokens1, mCurrentConfig, files, true);
11091109
});
11101110

11111111
if (startsWith(codeWithoutCfg,"#file"))
@@ -1131,7 +1131,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11311131
try {
11321132
// Create tokens, skip rest of iteration if failed
11331133
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1134-
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, file.lang(), true);
1134+
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, true);
11351135
tokenizer.list.createTokens(std::move(tokensP));
11361136
});
11371137
hasValidConfig = true;

lib/preprocessor.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <algorithm>
3333
#include <array>
34+
#include <cassert>
3435
#include <cstddef>
3536
#include <iterator>
3637
#include <sstream>
@@ -63,8 +64,13 @@ Directive::DirectiveToken::DirectiveToken(const simplecpp::Token & _tok) :
6364

6465
char Preprocessor::macroChar = char(1);
6566

66-
Preprocessor::Preprocessor(const Settings& settings, ErrorLogger &errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
67-
{}
67+
Preprocessor::Preprocessor(const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang)
68+
: mSettings(settings)
69+
, mErrorLogger(errorLogger)
70+
, mLang(lang)
71+
{
72+
assert(mLang != Standards::Language::None);
73+
}
6874

6975
Preprocessor::~Preprocessor()
7076
{
@@ -770,9 +776,9 @@ void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool th
770776
}
771777
}
772778

773-
bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, Standards::Language lang)
779+
bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files)
774780
{
775-
const simplecpp::DUI dui = createDUI(mSettings, "", lang);
781+
const simplecpp::DUI dui = createDUI(mSettings, "", mLang);
776782

777783
simplecpp::OutputList outputList;
778784
mTokenLists = simplecpp::load(rawtokens, files, dui, &outputList);
@@ -809,9 +815,9 @@ void Preprocessor::setPlatformInfo(simplecpp::TokenList &tokens, const Settings&
809815
tokens.sizeOfType["long double *"] = settings.platform.sizeof_pointer;
810816
}
811817

812-
simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, Standards::Language lang, bool throwError)
818+
simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError)
813819
{
814-
const simplecpp::DUI dui = createDUI(mSettings, cfg, lang);
820+
const simplecpp::DUI dui = createDUI(mSettings, cfg, mLang);
815821

816822
simplecpp::OutputList outputList;
817823
std::list<simplecpp::MacroUsage> macroUsage;
@@ -828,9 +834,9 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens
828834
return tokens2;
829835
}
830836

831-
std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, Standards::Language lang, const bool writeLocations)
837+
std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations)
832838
{
833-
simplecpp::TokenList tokens2 = preprocess(tokens1, cfg, files, lang, false);
839+
simplecpp::TokenList tokens2 = preprocess(tokens1, cfg, files, false);
834840
unsigned int prevfile = 0;
835841
unsigned int line = 1;
836842
std::ostringstream ret;
@@ -926,7 +932,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
926932

927933
void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &settings)
928934
{
929-
Preprocessor preprocessor(settings, errorLogger);
935+
Preprocessor preprocessor(settings, errorLogger, Standards::Language::CPP);
930936
preprocessor.missingInclude("", 1, "", UserHeader);
931937
preprocessor.missingInclude("", 1, "", SystemHeader);
932938
preprocessor.error("", 1, "#error message"); // #error ..

lib/preprocessor.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
109109
/** character that is inserted in expanded macros */
110110
static char macroChar;
111111

112-
explicit Preprocessor(const Settings& settings, ErrorLogger &errorLogger);
112+
explicit Preprocessor(const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang);
113113
virtual ~Preprocessor();
114114

115115
void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions);
@@ -120,15 +120,15 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
120120

121121
std::vector<RemarkComment> getRemarkComments(const simplecpp::TokenList &tokens) const;
122122

123-
bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, Standards::Language lang);
123+
bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);
124124

125125
void removeComments(simplecpp::TokenList &tokens);
126126

127127
static void setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings);
128128

129-
simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, Standards::Language lang, bool throwError = false);
129+
simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError = false);
130130

131-
std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, Standards::Language lang, bool writeLocations);
131+
std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool writeLocations);
132132

133133
/**
134134
* Calculate HASH. Using toolinfo, tokens1, filedata.
@@ -181,6 +181,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
181181

182182
/** filename for cpp/c file - useful when reporting errors */
183183
std::string mFile0;
184+
Standards::Language mLang{Standards::Language::None};
184185

185186
/** simplecpp tracking info */
186187
std::list<simplecpp::MacroUsage> mMacroUsage;

test/helpers.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
136136

137137
std::istringstream istr(code);
138138
simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
139-
Preprocessor preprocessor(settings, errorlogger);
139+
Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
140140
if (inlineSuppression)
141141
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
142142
preprocessor.removeComments(tokens);
@@ -168,7 +168,7 @@ void SimpleTokenizer2::preprocess(const char code[], std::vector<std::string> &f
168168
std::istringstream istr(code);
169169
const simplecpp::TokenList tokens1(istr, files, file0);
170170

171-
Preprocessor preprocessor(tokenizer.getSettings(), errorlogger);
171+
Preprocessor preprocessor(tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false));
172172
simplecpp::TokenList tokens2 = preprocessor.preprocess(tokens1, "", files, true);
173173

174174
// Tokenizer..

test/testpreprocessor.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TestPreprocessor : public TestFixture {
5454
simplecpp::OutputList outputList;
5555
std::vector<std::string> files;
5656
const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList);
57-
Preprocessor p(settingsDefault, errorLogger);
57+
Preprocessor p(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
5858
simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true);
5959
p.reportOutput(outputList, true);
6060
return tokens2.stringify();
@@ -87,7 +87,7 @@ class TestPreprocessor : public TestFixture {
8787
std::istringstream istr(code);
8888
const simplecpp::TokenList tokens1(istr, files, "test.cpp");
8989

90-
const Preprocessor preprocessor(settingsDefault, errorLogger);
90+
const Preprocessor preprocessor(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
9191
return preprocessor.getRemarkComments(tokens1);
9292
}
9393

@@ -300,11 +300,11 @@ class TestPreprocessor : public TestFixture {
300300
settings.userDefines = arg + 2;
301301
if (arg && std::strncmp(arg,"-U",2)==0)
302302
settings.userUndefs.insert(arg+2);
303-
Preprocessor preprocessor(settings, *this);
304303
std::vector<std::string> files;
305304
std::istringstream istr(filedata);
306305
simplecpp::TokenList tokens(istr,files);
307306
tokens.removeComments();
307+
Preprocessor preprocessor(settings, *this, Path::identify(tokens.getFiles()[0], false));
308308
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
309309
std::string ret;
310310
for (const std::string & config : configs)
@@ -313,11 +313,11 @@ class TestPreprocessor : public TestFixture {
313313
}
314314

315315
std::size_t getHash(const char filedata[]) {
316-
Preprocessor preprocessor(settingsDefault, *this);
317316
std::vector<std::string> files;
318317
std::istringstream istr(filedata);
319318
simplecpp::TokenList tokens(istr,files);
320319
tokens.removeComments();
320+
Preprocessor preprocessor(settingsDefault, *this, Path::identify(tokens.getFiles()[0], false));
321321
return preprocessor.calculateHash(tokens, "");
322322
}
323323

@@ -477,15 +477,15 @@ class TestPreprocessor : public TestFixture {
477477
{
478478
const Settings settings = settingsBuilder().platform(Platform::Type::Unix32).build();
479479
Preprocessor::setPlatformInfo(tokens, settings);
480-
Preprocessor preprocessor(settings, *this);
480+
Preprocessor preprocessor(settings, *this, Path::identify(tokens.getFiles()[0], false));
481481
ASSERT_EQUALS("\n1", preprocessor.getcode(tokens, "", files, false));
482482
}
483483

484484
// preprocess code with unix64 platform..
485485
{
486486
const Settings settings = settingsBuilder().platform(Platform::Type::Unix64).build();
487487
Preprocessor::setPlatformInfo(tokens, settings);
488-
Preprocessor preprocessor(settings, *this);
488+
Preprocessor preprocessor(settings, *this, Path::identify(tokens.getFiles()[0], false));
489489
ASSERT_EQUALS("\n\n\n2", preprocessor.getcode(tokens, "", files, false));
490490
}
491491
}

test/testtokenize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,11 @@ class TestTokenizer : public TestFixture {
551551
}
552552

553553
void directiveDump(const char filedata[], const char filename[], const Settings& settings, std::ostream& ostr) {
554-
Preprocessor preprocessor(settings, *this);
555554
std::istringstream istr(filedata);
556555
simplecpp::OutputList outputList;
557556
std::vector<std::string> files;
558557
const simplecpp::TokenList tokens1(istr, files, filename, &outputList);
558+
Preprocessor preprocessor(settings, *this, Path::identify(tokens1.getFiles()[0], false));
559559
std::list<Directive> directives = preprocessor.createDirectives(tokens1);
560560

561561
Tokenizer tokenizer(settings, *this);

test/testtokenlist.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class TestTokenList : public TestFixture {
165165
std::istringstream istr(code);
166166
std::vector<std::string> files;
167167
simplecpp::TokenList tokens1(istr, files, "poll.h", nullptr);
168-
Preprocessor preprocessor(settingsDefault, *this);
168+
Preprocessor preprocessor(settingsDefault, *this, Path::identify(tokens1.getFiles()[0], false));
169169
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, "", files, true);
170170
TokenList tokenlist(&settingsDefault);
171171
tokenlist.createTokens(std::move(tokensP)); // do not assert

0 commit comments

Comments
 (0)