diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 3d41048fe..e886572c7 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1498,6 +1498,8 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string // Create a map for the cfg for faster access to defines std::map cfgmap(getcfgmap(cfg)); + if (Path::isCPP(filename) && cfgmap.find("__cplusplus") == cfgmap.end()) + cfgmap["__cplusplus"] = "1"; std::stack filenames; filenames.push(filename); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 120615524..7450efa52 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -236,6 +236,7 @@ private: TEST_CASE(predefine2); TEST_CASE(predefine3); TEST_CASE(predefine4); + TEST_CASE(predefine5); // automatically define __cplusplus // Test Preprocessor::simplifyCondition TEST_CASE(simplifyCondition); @@ -2977,6 +2978,13 @@ private: ASSERT_EQUALS("char buf[$123];\n", actual); } + void predefine5() { // #3737 - automatically define __cplusplus + const char code[] = "#ifdef __cplusplus\n123\n#endif"; + Preprocessor preprocessor(NULL,this); + ASSERT_EQUALS("\n\n\n", preprocessor.getcode(code, "X=123", "test.c")); + ASSERT_EQUALS("\n123\n\n", preprocessor.getcode(code, "X=123", "test.cpp")); + } + void simplifyCondition() { // Ticket #2794 std::map cfg;