Skip to content

Commit 8c7e3d1

Browse files
committed
Fixed danmar#5119 (Preprocessor: Using -D suppresses __cplusplus for C++ files)
1 parent 30720af commit 8c7e3d1

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/preprocessor.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,18 @@ static std::string unify(const std::string &s, char separator)
123123
}
124124

125125

126+
bool Preprocessor::cplusplus(const Settings *settings, const std::string &filename)
127+
{
128+
const bool undef = settings && settings->userUndefs.find("__cplusplus") != settings->userUndefs.end();
129+
const bool cpplang = settings && settings->enforcedLang == Settings::CPP;
130+
const bool cppfile = (!settings || settings->enforcedLang == Settings::None) && Path::isCPP(filename);
131+
return (!undef && (cpplang || cppfile));
132+
}
133+
126134
/**
127135
* Get cfgmap - a map of macro names and values
128136
*/
129-
static std::map<std::string,std::string> getcfgmap(const std::string &cfg)
137+
static std::map<std::string,std::string> getcfgmap(const std::string &cfg, const Settings *settings, const std::string &filename)
130138
{
131139
std::map<std::string, std::string> cfgmap;
132140

@@ -154,6 +162,9 @@ static std::map<std::string,std::string> getcfgmap(const std::string &cfg)
154162
}
155163
}
156164

165+
if (cfgmap.find("__cplusplus") == cfgmap.end() && Preprocessor::cplusplus(settings,filename))
166+
cfgmap["__cplusplus"] = "1";
167+
157168
return cfgmap;
158169
}
159170

@@ -1023,7 +1034,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
10231034
processedFile = ostr.str();
10241035
}
10251036

1026-
std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : std::string("")));
1037+
std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : std::string(""), _settings, filename));
10271038

10281039
if (_settings && _settings->_maxConfigs == 1U) {
10291040
std::set<std::string> pragmaOnce;
@@ -1735,9 +1746,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
17351746
std::list<bool> matched_ifdef;
17361747

17371748
// Create a map for the cfg for faster access to defines
1738-
std::map<std::string, std::string> cfgmap(getcfgmap(cfg));
1739-
if (((_settings && _settings->enforcedLang == Settings::CPP) || ((!_settings || _settings->enforcedLang == Settings::None) && Path::isCPP(filename))) && cfgmap.find("__cplusplus") == cfgmap.end())
1740-
cfgmap["__cplusplus"] = "1";
1749+
std::map<std::string, std::string> cfgmap(getcfgmap(cfg, _settings, filename));
17411750

17421751
std::stack<std::string> filenames;
17431752
filenames.push(filename);
@@ -2895,7 +2904,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
28952904

28962905
{
28972906
// fill up "macros" with user defined macros
2898-
const std::map<std::string,std::string> cfgmap(getcfgmap(cfg));
2907+
const std::map<std::string,std::string> cfgmap(getcfgmap(cfg,NULL,""));
28992908
std::map<std::string, std::string>::const_iterator it;
29002909
for (it = cfgmap.begin(); it != cfgmap.end(); ++it) {
29012910
std::string s = it->first;

lib/preprocessor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class CPPCHECKLIB Preprocessor {
9696
/** read preprocessor statements into a string. */
9797
std::string readpreprocessor(std::istream &istr, const unsigned int bom) const;
9898

99+
/** should __cplusplus be defined? */
100+
static bool cplusplus(const Settings *settings, const std::string &filename);
101+
99102
/**
100103
* Get preprocessed code for a given configuration
101104
* @param filedata file data including preprocessing 'if', 'define', etc

test/testpreprocessor.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3272,11 +3272,21 @@ class TestPreprocessor : public TestFixture {
32723272
ASSERT_EQUALS("char buf[$123];\n", actual);
32733273
}
32743274

3275-
void predefine5() { // #3737 - automatically define __cplusplus
3275+
void predefine5() { // #3737, #5119 - automatically define __cplusplus
3276+
// #3737...
32763277
const char code[] = "#ifdef __cplusplus\n123\n#endif";
32773278
Preprocessor preprocessor(NULL,this);
32783279
ASSERT_EQUALS("\n\n\n", preprocessor.getcode(code, "X=123", "test.c"));
32793280
ASSERT_EQUALS("\n123\n\n", preprocessor.getcode(code, "X=123", "test.cpp"));
3281+
3282+
// #5119...
3283+
ASSERT_EQUALS(false, Preprocessor::cplusplus(NULL,"test.c"));
3284+
ASSERT_EQUALS(true, Preprocessor::cplusplus(NULL,"test.cpp"));
3285+
3286+
Settings settings;
3287+
ASSERT_EQUALS(true, Preprocessor::cplusplus(&settings,"test.cpp"));
3288+
settings.userUndefs.insert("__cplusplus");
3289+
ASSERT_EQUALS(false, Preprocessor::cplusplus(&settings,"test.cpp"));
32803290
}
32813291

32823292
void predefine6() { // #3737 - using -D and -f => check all matching configurations

0 commit comments

Comments
 (0)