@@ -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 ;
0 commit comments