diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 81d748469..72d7c00d1 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -709,133 +709,145 @@ static std::unordered_map createBuiltinLibr return v; }; functions["atan2"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::atan2(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["remainder"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::remainder(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["nextafter"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::nextafter(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["nexttoward"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::nexttoward(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["hypot"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::hypot(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["fdim"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::fdim(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["fmax"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::fmax(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["fmin"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::fmin(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["fmod"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::fmod(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["pow"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::pow(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["scalbln"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::scalbln(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; }; functions["ldexp"] = [](const std::vector& args) { - if (args.size() != 2) - return ValueFlow::Value::unknown(); - ValueFlow::Value v = args[0]; - if (!v.isFloatValue() && !v.isIntValue()) + if (args.size() != 2 || !std::all_of(args.begin(), args.end(), [](const ValueFlow::Value& v) { + return v.isFloatValue() || v.isIntValue(); + })) return ValueFlow::Value::unknown(); double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue; + ValueFlow::Value v; + combineValueProperties(args[0], args[1], &v); v.floatValue = std::ldexp(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue); v.valueType = ValueFlow::Value::ValueType::FLOAT; return v; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 43a97bcad..589fd4239 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -500,7 +500,7 @@ static bool isNumeric(const ValueFlow::Value& value) { return value.isIntValue() || value.isFloatValue(); } -static void combineValueProperties(const ValueFlow::Value &value1, const ValueFlow::Value &value2, ValueFlow::Value *result) +void combineValueProperties(const ValueFlow::Value &value1, const ValueFlow::Value &value2, ValueFlow::Value *result) { if (value1.isKnown() && value2.isKnown()) result->setKnown(); diff --git a/lib/valueflow.h b/lib/valueflow.h index 928bc783b..6b141ef17 100644 --- a/lib/valueflow.h +++ b/lib/valueflow.h @@ -535,4 +535,6 @@ CPPCHECKLIB std::vector getLifetimeObjValues(const Token* tok, const Token* getEndOfExprScope(const Token* tok, const Scope* defaultScope = nullptr, bool smallest = true); +void combineValueProperties(const ValueFlow::Value& value1, const ValueFlow::Value& value2, ValueFlow::Value* result); + #endif // valueflowH diff --git a/test/testother.cpp b/test/testother.cpp index 63fd58d32..7ba66b00f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -10347,6 +10347,12 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f(int i, int j) {\n" // #11191 + " const int c = pow(2, i);\n" + " if (j % c) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void sameExpressionPointers() { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index c2731c9f3..6d11e68fc 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -202,18 +202,6 @@ private: return tokenizer.tokens()->stringifyList(nullptr, false); } -#define tokWithNewlines(code) tokWithNewlines_(code, __FILE__, __LINE__) - std::string tokWithNewlines_(const char code[], const char* file, int line) { - errout.str(""); - - Tokenizer tokenizer(&settings0, this); - - std::istringstream istr(code); - ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line); - - return tokenizer.tokens()->stringifyList(false, false, false, true, false); - } - #define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__) std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) { errout.str(""); @@ -260,1338 +248,6 @@ private: } - void simplifyMathFunctions_erfc() { - // verify erfc(), erfcf(), erfcl() - simplifcation - const char code_erfc[] ="void f(int x) {\n" - " std::cout << erfc(x);\n" // do not simplify - " std::cout << erfc(0L);\n" // simplify to 1 - "}"; - const char expected_erfc[] = "void f ( int x ) {\n" - "std :: cout << erfc ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_erfc, tokWithNewlines(code_erfc)); - - const char code_erfcf[] ="void f(float x) {\n" - " std::cout << erfcf(x);\n" // do not simplify - " std::cout << erfcf(0.0f);\n" // simplify to 1 - "}"; - const char expected_erfcf[] = "void f ( float x ) {\n" - "std :: cout << erfcf ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_erfcf, tokWithNewlines(code_erfcf)); - - const char code_erfcl[] ="void f(long double x) {\n" - " std::cout << erfcl(x);\n" // do not simplify - " std::cout << erfcl(0.0f);\n" // simplify to 1 - "}"; - const char expected_erfcl[] = "void f ( double x ) {\n" - "std :: cout << erfcl ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_erfcl, tokWithNewlines(code_erfcl)); - } - - void simplifyMathFunctions_cos() { - // verify cos(), cosf(), cosl() - simplifcation - const char code_cos[] ="void f(int x) {\n" - " std::cout << cos(x);\n" // do not simplify - " std::cout << cos(0L);\n" // simplify to 1 - "}"; - const char expected_cos[] = "void f ( int x ) {\n" - "std :: cout << cos ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cos, tokWithNewlines(code_cos)); - - const char code_cosf[] ="void f(float x) {\n" - " std::cout << cosf(x);\n" // do not simplify - " std::cout << cosf(0.0f);\n" // simplify to 1 - "}"; - const char expected_cosf[] = "void f ( float x ) {\n" - "std :: cout << cosf ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cosf, tokWithNewlines(code_cosf)); - - const char code_cosl[] ="void f(long double x) {\n" - " std::cout << cosl(x);\n" // do not simplify - " std::cout << cosl(0.0f);\n" // simplify to 1 - "}"; - const char expected_cosl[] = "void f ( double x ) {\n" - "std :: cout << cosl ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cosl, tokWithNewlines(code_cosl)); - } - - void simplifyMathFunctions_cosh() { - // verify cosh(), coshf(), coshl() - simplifcation - const char code_cosh[] ="void f(int x) {\n" - " std::cout << cosh(x);\n" // do not simplify - " std::cout << cosh(0L);\n" // simplify to 1 - "}"; - const char expected_cosh[] = "void f ( int x ) {\n" - "std :: cout << cosh ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cosh, tokWithNewlines(code_cosh)); - - const char code_coshf[] ="void f(float x) {\n" - " std::cout << coshf(x);\n" // do not simplify - " std::cout << coshf(0.0f);\n" // simplify to 1 - "}"; - const char expected_coshf[] = "void f ( float x ) {\n" - "std :: cout << coshf ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_coshf, tokWithNewlines(code_coshf)); - - const char code_coshl[] ="void f(long double x) {\n" - " std::cout << coshl(x);\n" // do not simplify - " std::cout << coshl(0.0f);\n" // simplify to 1 - "}"; - const char expected_coshl[] = "void f ( double x ) {\n" - "std :: cout << coshl ( x ) ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_coshl, tokWithNewlines(code_coshl)); - } - - void simplifyMathFunctions_acos() { - // verify acos(), acosf(), acosl() - simplifcation - const char code_acos[] ="void f(int x) {\n" - " std::cout << acos(x);\n" // do not simplify - " std::cout << acos(1L);\n" // simplify to 0 - "}"; - const char expected_acos[] = "void f ( int x ) {\n" - "std :: cout << acos ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acos, tokWithNewlines(code_acos)); - - const char code_acosf[] ="void f(float x) {\n" - " std::cout << acosf(x);\n" // do not simplify - " std::cout << acosf(1.0f);\n" // simplify to 0 - "}"; - const char expected_acosf[] = "void f ( float x ) {\n" - "std :: cout << acosf ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acosf, tokWithNewlines(code_acosf)); - - const char code_acosl[] ="void f(long double x) {\n" - " std::cout << acosl(x);\n" // do not simplify - " std::cout << acosl(1.0f);\n" // simplify to 0 - "}"; - const char expected_acosl[] = "void f ( double x ) {\n" - "std :: cout << acosl ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acosl, tokWithNewlines(code_acosl)); - } - - void simplifyMathFunctions_acosh() { - // verify acosh(), acoshf(), acoshl() - simplifcation - const char code_acosh[] ="void f(int x) {\n" - " std::cout << acosh(x);\n" // do not simplify - " std::cout << acosh(1L);\n" // simplify to 0 - "}"; - const char expected_acosh[] = "void f ( int x ) {\n" - "std :: cout << acosh ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acosh, tokWithNewlines(code_acosh)); - - const char code_acoshf[] ="void f(float x) {\n" - " std::cout << acoshf(x);\n" // do not simplify - " std::cout << acoshf(1.0f);\n" // simplify to 0 - "}"; - const char expected_acoshf[] = "void f ( float x ) {\n" - "std :: cout << acoshf ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acoshf, tokWithNewlines(code_acoshf)); - - const char code_acoshl[] ="void f(long double x) {\n" - " std::cout << acoshl(x);\n" // do not simplify - " std::cout << acoshl(1.0f);\n" // simplify to 0 - "}"; - const char expected_acoshl[] = "void f ( double x ) {\n" - "std :: cout << acoshl ( x ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_acoshl, tokWithNewlines(code_acoshl)); - } - - void simplifyMathFunctions_sqrt() { - // verify sqrt(), sqrtf(), sqrtl() - simplifcation - const char code_sqrt[] ="void f(int x) {\n" - " std::cout << sqrt(x);\n" // do not simplify - " std::cout << sqrt(-1);\n" // do not simplify - " std::cout << sqrt(0L);\n" // simplify to 0 - " std::cout << sqrt(1L);\n" // simplify to 1 - "}"; - const char expected_sqrt[] = "void f ( int x ) {\n" - "std :: cout << sqrt ( x ) ;\n" - "std :: cout << sqrt ( -1 ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_sqrt, tokWithNewlines(code_sqrt)); - - const char code_sqrtf[] ="void f(float x) {\n" - " std::cout << sqrtf(x);\n" // do not simplify - " std::cout << sqrtf(-1.0f);\n" // do not simplify - " std::cout << sqrtf(0.0f);\n" // simplify to 0 - " std::cout << sqrtf(1.0);\n" // simplify to 1 - "}"; - const char expected_sqrtf[] = "void f ( float x ) {\n" - "std :: cout << sqrtf ( x ) ;\n" - "std :: cout << sqrtf ( -1.0f ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_sqrtf, tokWithNewlines(code_sqrtf)); - - const char code_sqrtl[] ="void f(long double x) {\n" - " std::cout << sqrtf(x);\n" // do not simplify - " std::cout << sqrtf(-1.0);\n" // do not simplify - " std::cout << sqrtf(0.0);\n" // simplify to 0 - " std::cout << sqrtf(1.0);\n" // simplify to 1 - "}"; - const char expected_sqrtl[] = "void f ( double x ) {\n" - "std :: cout << sqrtf ( x ) ;\n" - "std :: cout << sqrtf ( -1.0 ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_sqrtl, tokWithNewlines(code_sqrtl)); - } - - void simplifyMathFunctions_cbrt() { - // verify cbrt(), cbrtf(), cbrtl() - simplifcation - const char code_cbrt[] ="void f(int x) {\n" - " std::cout << cbrt(x);\n" // do not simplify - " std::cout << cbrt(-1);\n" // do not simplify - " std::cout << cbrt(0L);\n" // simplify to 0 - " std::cout << cbrt(1L);\n" // simplify to 1 - "}"; - const char expected_cbrt[] = "void f ( int x ) {\n" - "std :: cout << cbrt ( x ) ;\n" - "std :: cout << cbrt ( -1 ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cbrt, tokWithNewlines(code_cbrt)); - - const char code_cbrtf[] ="void f(float x) {\n" - " std::cout << cbrtf(x);\n" // do not simplify - " std::cout << cbrtf(-1.0f);\n" // do not simplify - " std::cout << cbrtf(0.0f);\n" // simplify to 0 - " std::cout << cbrtf(1.0);\n" // simplify to 1 - "}"; - const char expected_cbrtf[] = "void f ( float x ) {\n" - "std :: cout << cbrtf ( x ) ;\n" - "std :: cout << cbrtf ( -1.0f ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cbrtf, tokWithNewlines(code_cbrtf)); - - const char code_cbrtl[] ="void f(long double x) {\n" - " std::cout << cbrtl(x);\n" // do not simplify - " std::cout << cbrtl(-1.0);\n" // do not simplify - " std::cout << cbrtl(0.0);\n" // simplify to 0 - " std::cout << cbrtl(1.0);\n" // simplify to 1 - "}"; - const char expected_cbrtl[] = "void f ( double x ) {\n" - "std :: cout << cbrtl ( x ) ;\n" - "std :: cout << cbrtl ( -1.0 ) ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_cbrtl, tokWithNewlines(code_cbrtl)); - } - - void simplifyMathFunctions_exp2() { - // verify exp2(), exp2f(), exp2l() - simplifcation - const char code_exp2[] ="void f(int x) {\n" - " std::cout << exp2(x);\n" // do not simplify - " std::cout << exp2(-1);\n" // do not simplify - " std::cout << exp2(0L);\n" // simplify to 0 - " std::cout << exp2(1L);\n" // do not simplify - "}"; - const char expected_exp2[] = "void f ( int x ) {\n" - "std :: cout << exp2 ( x ) ;\n" - "std :: cout << exp2 ( -1 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << exp2 ( 1L ) ;\n" - "}"; - ASSERT_EQUALS(expected_exp2, tokWithNewlines(code_exp2)); - - const char code_exp2f[] ="void f(float x) {\n" - " std::cout << exp2f(x);\n" // do not simplify - " std::cout << exp2f(-1.0);\n" // do not simplify - " std::cout << exp2f(0.0);\n" // simplify to 1 - " std::cout << exp2f(1.0);\n" // do not simplify - "}"; - const char expected_exp2f[] = "void f ( float x ) {\n" - "std :: cout << exp2f ( x ) ;\n" - "std :: cout << exp2f ( -1.0 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << exp2f ( 1.0 ) ;\n" - "}"; - ASSERT_EQUALS(expected_exp2f, tokWithNewlines(code_exp2f)); - - const char code_exp2l[] ="void f(long double x) {\n" - " std::cout << exp2l(x);\n" // do not simplify - " std::cout << exp2l(-1.0);\n" // do not simplify - " std::cout << exp2l(0.0);\n" // simplify to 1 - " std::cout << exp2l(1.0);\n" // do not simplify - "}"; - const char expected_exp2l[] = "void f ( double x ) {\n" - "std :: cout << exp2l ( x ) ;\n" - "std :: cout << exp2l ( -1.0 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << exp2l ( 1.0 ) ;\n" - "}"; - ASSERT_EQUALS(expected_exp2l, tokWithNewlines(code_exp2l)); - } - - void simplifyMathFunctions_exp() { - // verify exp(), expf(), expl() - simplifcation - const char code_exp[] ="void f(int x) {\n" - " std::cout << exp(x);\n" // do not simplify - " std::cout << exp(-1);\n" // do not simplify - " std::cout << exp(0L);\n" // simplify to 1 - " std::cout << exp(1L);\n" // do not simplify - "}"; - const char expected_exp[] = "void f ( int x ) {\n" - "std :: cout << exp ( x ) ;\n" - "std :: cout << exp ( -1 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << exp ( 1L ) ;\n" - "}"; - ASSERT_EQUALS(expected_exp, tokWithNewlines(code_exp)); - - const char code_expf[] ="void f(float x) {\n" - " std::cout << expf(x);\n" // do not simplify - " std::cout << expf(-1.0);\n" // do not simplify - " std::cout << expf(0.0);\n" // simplify to 1 - " std::cout << expf(1.0);\n" // do not simplify - "}"; - const char expected_expf[] = "void f ( float x ) {\n" - "std :: cout << expf ( x ) ;\n" - "std :: cout << expf ( -1.0 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << expf ( 1.0 ) ;\n" - "}"; - ASSERT_EQUALS(expected_expf, tokWithNewlines(code_expf)); - - const char code_expl[] ="void f(long double x) {\n" - " std::cout << expl(x);\n" // do not simplify - " std::cout << expl(-1.0);\n" // do not simplify - " std::cout << expl(0.0);\n" // simplify to 1 - " std::cout << expl(1.0);\n" // do not simplify - "}"; - const char expected_expl[] = "void f ( double x ) {\n" - "std :: cout << expl ( x ) ;\n" - "std :: cout << expl ( -1.0 ) ;\n" - "std :: cout << 1 ;\n" - "std :: cout << expl ( 1.0 ) ;\n" - "}"; - ASSERT_EQUALS(expected_expl, tokWithNewlines(code_expl)); - } - - void simplifyMathFunctions_erf() { - // verify erf(), erff(), erfl() - simplifcation - const char code_erf[] ="void f(int x) {\n" - " std::cout << erf(x);\n" // do not simplify - " std::cout << erf(10);\n" // do not simplify - " std::cout << erf(0L);\n" // simplify to 0 - "}"; - const char expected_erf[] = "void f ( int x ) {\n" - "std :: cout << erf ( x ) ;\n" - "std :: cout << erf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_erf, tokWithNewlines(code_erf)); - - const char code_erff[] ="void f(float x) {\n" - " std::cout << erff(x);\n" // do not simplify - " std::cout << erff(10);\n" // do not simplify - " std::cout << erff(0.0f);\n" // simplify to 0 - "}"; - const char expected_erff[] = "void f ( float x ) {\n" - "std :: cout << erff ( x ) ;\n" - "std :: cout << erff ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_erff, tokWithNewlines(code_erff)); - - const char code_erfl[] ="void f(long double x) {\n" - " std::cout << erfl(x);\n" // do not simplify - " std::cout << erfl(10.0f);\n" // do not simplify - " std::cout << erfl(0.0f);\n" // simplify to 0 - "}"; - const char expected_erfl[] = "void f ( double x ) {\n" - "std :: cout << erfl ( x ) ;\n" - "std :: cout << erfl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_erfl, tokWithNewlines(code_erfl)); - } - - void simplifyMathFunctions_atanh() { - // verify atanh(), atanhf(), atanhl() - simplifcation - const char code_atanh[] ="void f(int x) {\n" - " std::cout << atanh(x);\n" // do not simplify - " std::cout << atanh(10);\n" // do not simplify - " std::cout << atanh(0L);\n" // simplify to 0 - "}"; - const char expected_atanh[] = "void f ( int x ) {\n" - "std :: cout << atanh ( x ) ;\n" - "std :: cout << atanh ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atanh, tokWithNewlines(code_atanh)); - - const char code_atanhf[] ="void f(float x) {\n" - " std::cout << atanhf(x);\n" // do not simplify - " std::cout << atanhf(10);\n" // do not simplify - " std::cout << atanhf(0.0f);\n" // simplify to 0 - "}"; - const char expected_atanhf[] = "void f ( float x ) {\n" - "std :: cout << atanhf ( x ) ;\n" - "std :: cout << atanhf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atanhf, tokWithNewlines(code_atanhf)); - - const char code_atanhl[] ="void f(long double x) {\n" - " std::cout << atanhl(x);\n" // do not simplify - " std::cout << atanhl(10.0f);\n" // do not simplify - " std::cout << atanhl(0.0d);\n" // do not simplify - invalid number! - " std::cout << atanhl(0.0f);\n" // simplify to 0 - "}"; - const char expected_atanhl[] = "void f ( double x ) {\n" - "std :: cout << atanhl ( x ) ;\n" - "std :: cout << atanhl ( 10.0f ) ;\n" - "std :: cout << atanhl ( 0.0d ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atanhl, tokWithNewlines(code_atanhl)); - } - - void simplifyMathFunctions_atan() { - // verify atan(), atanf(), atanl() - simplifcation - const char code_atan[] ="void f(int x) {\n" - " std::cout << atan(x);\n" // do not simplify - " std::cout << atan(10);\n" // do not simplify - " std::cout << atan(0L);\n" // simplify to 0 - "}"; - const char expected_atan[] = "void f ( int x ) {\n" - "std :: cout << atan ( x ) ;\n" - "std :: cout << atan ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atan, tokWithNewlines(code_atan)); - - const char code_atanf[] ="void f(float x) {\n" - " std::cout << atanf(x);\n" // do not simplify - " std::cout << atanf(10);\n" // do not simplify - " std::cout << atanf(0.0f);\n" // simplify to 0 - "}"; - const char expected_atanf[] = "void f ( float x ) {\n" - "std :: cout << atanf ( x ) ;\n" - "std :: cout << atanf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atanf, tokWithNewlines(code_atanf)); - - const char code_atanl[] ="void f(long double x) {\n" - " std::cout << atanl(x);\n" // do not simplify - " std::cout << atanl(10.0f);\n" // do not simplify - " std::cout << atanl(0.0f);\n" // simplify to 0 - "}"; - const char expected_atanl[] = "void f ( double x ) {\n" - "std :: cout << atanl ( x ) ;\n" - "std :: cout << atanl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_atanl, tokWithNewlines(code_atanl)); - } - - void simplifyMathFunctions_tanh() { - // verify tanh(), tanhf(), tanhl() - simplifcation - const char code_tanh[] ="void f(int x) {\n" - " std::cout << tanh(x);\n" // do not simplify - " std::cout << tanh(10);\n" // do not simplify - " std::cout << tanh(0L);\n" // simplify to 0 - "}"; - const char expected_tanh[] = "void f ( int x ) {\n" - "std :: cout << tanh ( x ) ;\n" - "std :: cout << tanh ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tanh, tokWithNewlines(code_tanh)); - - const char code_tanhf[] ="void f(float x) {\n" - " std::cout << tanhf(x);\n" // do not simplify - " std::cout << tanhf(10);\n" // do not simplify - " std::cout << tanhf(0.0f);\n" // simplify to 0 - "}"; - const char expected_tanhf[] = "void f ( float x ) {\n" - "std :: cout << tanhf ( x ) ;\n" - "std :: cout << tanhf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tanhf, tokWithNewlines(code_tanhf)); - - const char code_tanhl[] ="void f(long double x) {\n" - " std::cout << tanhl(x);\n" // do not simplify - " std::cout << tanhl(10.0f);\n" // do not simplify - " std::cout << tanhl(0.0f);\n" // simplify to 0 - "}"; - const char expected_tanhl[] = "void f ( double x ) {\n" - "std :: cout << tanhl ( x ) ;\n" - "std :: cout << tanhl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tanhl, tokWithNewlines(code_tanhl)); - } - - void simplifyMathFunctions_tan() { - // verify tan(), tanf(), tanl() - simplifcation - const char code_tan[] ="void f(int x) {\n" - " std::cout << tan(x);\n" // do not simplify - " std::cout << tan(10);\n" // do not simplify - " std::cout << tan(0L);\n" // simplify to 0 - "}"; - const char expected_tan[] = "void f ( int x ) {\n" - "std :: cout << tan ( x ) ;\n" - "std :: cout << tan ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tan, tokWithNewlines(code_tan)); - - const char code_tanf[] ="void f(float x) {\n" - " std::cout << tanf(x);\n" // do not simplify - " std::cout << tanf(10);\n" // do not simplify - " std::cout << tanf(0.0f);\n" // simplify to 0 - "}"; - const char expected_tanf[] = "void f ( float x ) {\n" - "std :: cout << tanf ( x ) ;\n" - "std :: cout << tanf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tanf, tokWithNewlines(code_tanf)); - - const char code_tanl[] ="void f(long double x) {\n" - " std::cout << tanl(x);\n" // do not simplify - " std::cout << tanl(10.0f);\n" // do not simplify - " std::cout << tanl(0.0f);\n" // simplify to 0 - "}"; - const char expected_tanl[] = "void f ( double x ) {\n" - "std :: cout << tanl ( x ) ;\n" - "std :: cout << tanl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_tanl, tokWithNewlines(code_tanl)); - } - - void simplifyMathFunctions_expm1() { - // verify expm1(), expm1f(), expm1l() - simplifcation - const char code_expm1[] ="void f(int x) {\n" - " std::cout << expm1(x);\n" // do not simplify - " std::cout << expm1(10);\n" // do not simplify - " std::cout << expm1(0L);\n" // simplify to 0 - "}"; - const char expected_expm1[] = "void f ( int x ) {\n" - "std :: cout << expm1 ( x ) ;\n" - "std :: cout << expm1 ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_expm1, tokWithNewlines(code_expm1)); - - const char code_expm1f[] ="void f(float x) {\n" - " std::cout << expm1f(x);\n" // do not simplify - " std::cout << expm1f(10);\n" // do not simplify - " std::cout << expm1f(0.0f);\n" // simplify to 0 - "}"; - const char expected_expm1f[] = "void f ( float x ) {\n" - "std :: cout << expm1f ( x ) ;\n" - "std :: cout << expm1f ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_expm1f, tokWithNewlines(code_expm1f)); - - const char code_expm1l[] ="void f(long double x) {\n" - " std::cout << expm1l(x);\n" // do not simplify - " std::cout << expm1l(10.0f);\n" // do not simplify - " std::cout << expm1l(0.0f);\n" // simplify to 0 - "}"; - const char expected_expm1l[] = "void f ( double x ) {\n" - "std :: cout << expm1l ( x ) ;\n" - "std :: cout << expm1l ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_expm1l, tokWithNewlines(code_expm1l)); - } - - void simplifyMathFunctions_asinh() { - // verify asinh(), asinhf(), asinhl() - simplifcation - const char code_asinh[] ="void f(int x) {\n" - " std::cout << asinh(x);\n" // do not simplify - " std::cout << asinh(10);\n" // do not simplify - " std::cout << asinh(0L);\n" // simplify to 0 - "}"; - const char expected_asinh[] = "void f ( int x ) {\n" - "std :: cout << asinh ( x ) ;\n" - "std :: cout << asinh ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asinh, tokWithNewlines(code_asinh)); - - const char code_asinhf[] ="void f(float x) {\n" - " std::cout << asinhf(x);\n" // do not simplify - " std::cout << asinhf(10);\n" // do not simplify - " std::cout << asinhf(0.0f);\n" // simplify to 0 - "}"; - const char expected_asinhf[] = "void f ( float x ) {\n" - "std :: cout << asinhf ( x ) ;\n" - "std :: cout << asinhf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asinhf, tokWithNewlines(code_asinhf)); - - const char code_asinhl[] ="void f(long double x) {\n" - " std::cout << asinhl(x);\n" // do not simplify - " std::cout << asinhl(10.0f);\n" // do not simplify - " std::cout << asinhl(0.0f);\n" // simplify to 0 - "}"; - const char expected_asinhl[] = "void f ( double x ) {\n" - "std :: cout << asinhl ( x ) ;\n" - "std :: cout << asinhl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asinhl, tokWithNewlines(code_asinhl)); - } - - void simplifyMathFunctions_asin() { - // verify asin(), asinf(), asinl() - simplifcation - const char code_asin[] ="void f(int x) {\n" - " std::cout << asin(x);\n" // do not simplify - " std::cout << asin(10);\n" // do not simplify - " std::cout << asin(0L);\n" // simplify to 0 - "}"; - const char expected_asin[] = "void f ( int x ) {\n" - "std :: cout << asin ( x ) ;\n" - "std :: cout << asin ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asin, tokWithNewlines(code_asin)); - - const char code_asinf[] ="void f(float x) {\n" - " std::cout << asinf(x);\n" // do not simplify - " std::cout << asinf(10);\n" // do not simplify - " std::cout << asinf(0.0f);\n" // simplify to 0 - "}"; - const char expected_asinf[] = "void f ( float x ) {\n" - "std :: cout << asinf ( x ) ;\n" - "std :: cout << asinf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asinf, tokWithNewlines(code_asinf)); - - const char code_asinl[] ="void f(long double x) {\n" - " std::cout << asinl(x);\n" // do not simplify - " std::cout << asinl(10.0f);\n" // do not simplify - " std::cout << asinl(0.0f);\n" // simplify to 0 - "}"; - const char expected_asinl[] = "void f ( double x ) {\n" - "std :: cout << asinl ( x ) ;\n" - "std :: cout << asinl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_asinl, tokWithNewlines(code_asinl)); - } - - void simplifyMathFunctions_sinh() { - // verify sinh(), sinhf(), sinhl() - simplifcation - const char code_sinh[] ="void f(int x) {\n" - " std::cout << sinh(x);\n" // do not simplify - " std::cout << sinh(10);\n" // do not simplify - " std::cout << sinh(0L);\n" // simplify to 0 - "}"; - const char expected_sinh[] = "void f ( int x ) {\n" - "std :: cout << sinh ( x ) ;\n" - "std :: cout << sinh ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sinh, tokWithNewlines(code_sinh)); - - const char code_sinhf[] ="void f(float x) {\n" - " std::cout << sinhf(x);\n" // do not simplify - " std::cout << sinhf(10);\n" // do not simplify - " std::cout << sinhf(0.0f);\n" // simplify to 0 - "}"; - const char expected_sinhf[] = "void f ( float x ) {\n" - "std :: cout << sinhf ( x ) ;\n" - "std :: cout << sinhf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sinhf, tokWithNewlines(code_sinhf)); - - const char code_sinhl[] ="void f(long double x) {\n" - " std::cout << sinhl(x);\n" // do not simplify - " std::cout << sinhl(10.0f);\n" // do not simplify - " std::cout << sinhl(0.0f);\n" // simplify to 0 - "}"; - const char expected_sinhl[] = "void f ( double x ) {\n" - "std :: cout << sinhl ( x ) ;\n" - "std :: cout << sinhl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sinhl, tokWithNewlines(code_sinhl)); - } - - void simplifyMathFunctions_sin() { - // verify sin(), sinf(), sinl() - simplifcation - const char code_sin[] ="void f(int x) {\n" - " std::cout << sin(x);\n" // do not simplify - " std::cout << sin(10);\n" // do not simplify - " std::cout << sin(0L);\n" // simplify to 0 - "}"; - const char expected_sin[] = "void f ( int x ) {\n" - "std :: cout << sin ( x ) ;\n" - "std :: cout << sin ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sin, tokWithNewlines(code_sin)); - - const char code_sinf[] ="void f(float x) {\n" - " std::cout << sinf(x);\n" // do not simplify - " std::cout << sinf(10);\n" // do not simplify - " std::cout << sinf(0.0f);\n" // simplify to 0 - "}"; - const char expected_sinf[] = "void f ( float x ) {\n" - "std :: cout << sinf ( x ) ;\n" - "std :: cout << sinf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sinf, tokWithNewlines(code_sinf)); - - const char code_sinl[] ="void f(long double x) {\n" - " std::cout << sinl(x);\n" // do not simplify - " std::cout << sinl(10.0f);\n" // do not simplify - " std::cout << sinl(0.0f);\n" // simplify to 0 - "}"; - const char expected_sinl[] = "void f ( double x ) {\n" - "std :: cout << sinl ( x ) ;\n" - "std :: cout << sinl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_sinl, tokWithNewlines(code_sinl)); - - // #6629 - const char code[] = "class Foo { int sinf; Foo() : sinf(0) {} };"; - const char expected[] = "class Foo { int sinf ; Foo ( ) : sinf ( 0 ) { } } ;"; - ASSERT_EQUALS(expected, tokWithNewlines(code)); - } - - void simplifyMathFunctions_ilogb() { - // verify ilogb(), ilogbf(), ilogbl() - simplifcation - const char code_ilogb[] ="void f(int x) {\n" - " std::cout << ilogb(x);\n" // do not simplify - " std::cout << ilogb(10);\n" // do not simplify - " std::cout << ilogb(1L);\n" // simplify to 0 - "}"; - const char expected_ilogb[] = "void f ( int x ) {\n" - "std :: cout << ilogb ( x ) ;\n" - "std :: cout << ilogb ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_ilogb, tokWithNewlines(code_ilogb)); - - const char code_ilogbf[] ="void f(float x) {\n" - " std::cout << ilogbf(x);\n" // do not simplify - " std::cout << ilogbf(10);\n" // do not simplify - " std::cout << ilogbf(1.0f);\n" // simplify to 0 - "}"; - const char expected_ilogbf[] = "void f ( float x ) {\n" - "std :: cout << ilogbf ( x ) ;\n" - "std :: cout << ilogbf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_ilogbf, tokWithNewlines(code_ilogbf)); - - const char code_ilogbl[] ="void f(long double x) {\n" - " std::cout << ilogbl(x);\n" // do not simplify - " std::cout << ilogbl(10.0f);\n" // do not simplify - " std::cout << ilogbl(1.0f);\n" // simplify to 0 - "}"; - const char expected_ilogbl[] = "void f ( double x ) {\n" - "std :: cout << ilogbl ( x ) ;\n" - "std :: cout << ilogbl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_ilogbl, tokWithNewlines(code_ilogbl)); - } - - void simplifyMathFunctions_logb() { - // verify logb(), logbf(), logbl() - simplifcation - const char code_logb[] ="void f(int x) {\n" - " std::cout << logb(x);\n" // do not simplify - " std::cout << logb(10);\n" // do not simplify - " std::cout << logb(1L);\n" // simplify to 0 - "}"; - const char expected_logb[] = "void f ( int x ) {\n" - "std :: cout << logb ( x ) ;\n" - "std :: cout << logb ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_logb, tokWithNewlines(code_logb)); - - const char code_logbf[] ="void f(float x) {\n" - " std::cout << logbf(x);\n" // do not simplify - " std::cout << logbf(10);\n" // do not simplify - " std::cout << logbf(1.0f);\n" // simplify to 0 - "}"; - const char expected_logbf[] = "void f ( float x ) {\n" - "std :: cout << logbf ( x ) ;\n" - "std :: cout << logbf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_logbf, tokWithNewlines(code_logbf)); - - const char code_logbl[] ="void f(long double x) {\n" - " std::cout << logbl(x);\n" // do not simplify - " std::cout << logbl(10.0f);\n" // do not simplify - " std::cout << logbl(1.0f);\n" // simplify to 0 - "}"; - const char expected_logbl[] = "void f ( double x ) {\n" - "std :: cout << logbl ( x ) ;\n" - "std :: cout << logbl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_logbl, tokWithNewlines(code_logbl)); - } - - void simplifyMathFunctions_log1p() { - // verify log1p(), log1pf(), log1pl() - simplifcation - const char code_log1p[] ="void f(int x) {\n" - " std::cout << log1p(x);\n" // do not simplify - " std::cout << log1p(10);\n" // do not simplify - " std::cout << log1p(0L);\n" // simplify to 0 - "}"; - const char expected_log1p[] = "void f ( int x ) {\n" - "std :: cout << log1p ( x ) ;\n" - "std :: cout << log1p ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log1p, tokWithNewlines(code_log1p)); - - const char code_log1pf[] ="void f(float x) {\n" - " std::cout << log1pf(x);\n" // do not simplify - " std::cout << log1pf(10);\n" // do not simplify - " std::cout << log1pf(0.0f);\n" // simplify to 0 - "}"; - const char expected_log1pf[] = "void f ( float x ) {\n" - "std :: cout << log1pf ( x ) ;\n" - "std :: cout << log1pf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log1pf, tokWithNewlines(code_log1pf)); - - const char code_log1pl[] ="void f(long double x) {\n" - " std::cout << log1pl(x);\n" // do not simplify - " std::cout << log1pl(10.0f);\n" // do not simplify - " std::cout << log1pl(0.0f);\n" // simplify to 0 - "}"; - const char expected_log1pl[] = "void f ( double x ) {\n" - "std :: cout << log1pl ( x ) ;\n" - "std :: cout << log1pl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log1pl, tokWithNewlines(code_log1pl)); - } - - void simplifyMathFunctions_log10() { - // verify log10(), log10f(), log10l() - simplifcation - const char code_log10[] ="void f(int x) {\n" - " std::cout << log10(x);\n" // do not simplify - " std::cout << log10(10);\n" // do not simplify - " std::cout << log10(1L);\n" // simplify to 0 - "}"; - const char expected_log10[] = "void f ( int x ) {\n" - "std :: cout << log10 ( x ) ;\n" - "std :: cout << log10 ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log10, tokWithNewlines(code_log10)); - - const char code_log10f[] ="void f(float x) {\n" - " std::cout << log10f(x);\n" // do not simplify - " std::cout << log10f(10);\n" // do not simplify - " std::cout << log10f(1.0f);\n" // simplify to 0 - "}"; - const char expected_log10f[] = "void f ( float x ) {\n" - "std :: cout << log10f ( x ) ;\n" - "std :: cout << log10f ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log10f, tokWithNewlines(code_log10f)); - - const char code_log10l[] ="void f(long double x) {\n" - " std::cout << log10l(x);\n" // do not simplify - " std::cout << log10l(10.0f);\n" // do not simplify - " std::cout << log10l(1.0f);\n" // simplify to 0 - "}"; - const char expected_log10l[] = "void f ( double x ) {\n" - "std :: cout << log10l ( x ) ;\n" - "std :: cout << log10l ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log10l, tokWithNewlines(code_log10l)); - - } - void simplifyMathFunctions_log() { - // verify log(), logf(), logl() - simplifcation - const char code_log[] ="void f(int x) {\n" - " std::cout << log(x);\n" // do not simplify - " std::cout << log(10);\n" // do not simplify - " std::cout << log(1L);\n" // simplify to 0 - "}"; - const char expected_log[] = "void f ( int x ) {\n" - "std :: cout << log ( x ) ;\n" - "std :: cout << log ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log, tokWithNewlines(code_log)); - - const char code_logf[] ="void f(float x) {\n" - " std::cout << logf(x);\n" // do not simplify - " std::cout << logf(10);\n" // do not simplify - " std::cout << logf(1.0f);\n" // simplify to 0 - "}"; - const char expected_logf[] = "void f ( float x ) {\n" - "std :: cout << logf ( x ) ;\n" - "std :: cout << logf ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_logf, tokWithNewlines(code_logf)); - - const char code_logl[] ="void f(long double x) {\n" - " std::cout << logl(x);\n" // do not simplify - " std::cout << logl(10.0f);\n" // do not simplify - " std::cout << logl(1.0f);\n" // simplify to 0 - "}"; - const char expected_logl[] = "void f ( double x ) {\n" - "std :: cout << logl ( x ) ;\n" - "std :: cout << logl ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_logl, tokWithNewlines(code_logl)); - } - - void simplifyMathFunctions_log2() { - // verify log2(), log2f(), log2l() - simplifcation - const char code_log2[] ="void f(int x) {\n" - " std::cout << log2(x);\n" // do not simplify - " std::cout << log2(10);\n" // do not simplify - " std::cout << log2(1L);\n" // simplify to 0 - "}"; - const char expected_log2[] = "void f ( int x ) {\n" - "std :: cout << log2 ( x ) ;\n" - "std :: cout << log2 ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log2, tokWithNewlines(code_log2)); - - const char code_log2f[] ="void f(float x) {\n" - " std::cout << log2f(x);\n" // do not simplify - " std::cout << log2f(10);\n" // do not simplify - " std::cout << log2f(1.0f);\n" // simplify to 0 - "}"; - const char expected_log2f[] = "void f ( float x ) {\n" - "std :: cout << log2f ( x ) ;\n" - "std :: cout << log2f ( 10 ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log2f, tokWithNewlines(code_log2f)); - - const char code_log2l[] ="void f(long double x) {\n" - " std::cout << log2l(x);\n" // do not simplify - " std::cout << log2l(10.0f);\n" // do not simplify - " std::cout << log2l(1.0f);\n" // simplify to 0 - "}"; - const char expected_log2l[] = "void f ( double x ) {\n" - "std :: cout << log2l ( x ) ;\n" - "std :: cout << log2l ( 10.0f ) ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_log2l, tokWithNewlines(code_log2l)); - } - - void simplifyMathFunctions_pow() { - // verify pow(),pow(),powl() - simplifcation - const char code_pow[] ="void f() {\n" - " std::cout << pow(-1.0,1);\n" - " std::cout << pow(1.0,1);\n" - " std::cout << pow(0,1);\n" - " std::cout << pow(1,-6);\n" - " std::cout << powf(-1.0,1.0f);\n" - " std::cout << powf(1.0,1.0f);\n" - " std::cout << powf(0,1.0f);\n" - " std::cout << powf(1.0,-6.0f);\n" - " std::cout << powl(-1.0,1.0);\n" - " std::cout << powl(1.0,1.0);\n" - " std::cout << powl(0,1.0);\n" - " std::cout << powl(1.0,-6.0d);\n" - "}"; - - const char expected_pow[] = "void f ( ) {\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 1 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 1 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 1 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1 ;\n" - "}"; - ASSERT_EQUALS(expected_pow, tokWithNewlines(code_pow)); - - // verify if code is simplified correctly. - // Do not simplify class members. - const char code_pow1[] = "int f(const Fred &fred) {return fred.pow(12,3);}"; - const char expected_pow1[] = "int f ( const Fred & fred ) { return fred . pow ( 12 , 3 ) ; }"; - ASSERT_EQUALS(expected_pow1, tokWithNewlines(code_pow1)); - - const char code_pow2[] = "int f() {return pow(0,0);}"; - const char expected_pow2[] = "int f ( ) { return 1 ; }"; - ASSERT_EQUALS(expected_pow2, tokWithNewlines(code_pow2)); - - const char code_pow3[] = "int f() {return pow(0,1);}"; - const char expected_pow3[] = "int f ( ) { return 0 ; }"; - ASSERT_EQUALS(expected_pow3, tokWithNewlines(code_pow3)); - - const char code_pow4[] = "int f() {return pow(1,0);}"; - const char expected_pow4[] = "int f ( ) { return 1 ; }"; - ASSERT_EQUALS(expected_pow4, tokWithNewlines(code_pow4)); - } - - void simplifyMathFunctions_fmin() { - // verify fmin,fminl,fminl simplifcation - const char code_fmin[] ="void f() {\n" - " std::cout << fmin(-1.0,0);\n" - " std::cout << fmin(1.0,0);\n" - " std::cout << fmin(0,0);\n" - " std::cout << fminf(-1.0,0);\n" - " std::cout << fminf(1.0,0);\n" - " std::cout << fminf(0,0);\n" - " std::cout << fminl(-1.0,0);\n" - " std::cout << fminl(1.0,0);\n" - " std::cout << fminl(0,0);\n" - "}"; - - const char expected_fmin[] = "void f ( ) {\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << -1.0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_fmin, tokWithNewlines(code_fmin)); - - // do not simplify this case - const char code_fmin1[] = "float f(float f) { return fmin(f,0);}"; - const char expected_fmin1[] = "float f ( float f ) { return fmin ( f , 0 ) ; }"; - ASSERT_EQUALS(expected_fmin1, tokWithNewlines(code_fmin1)); - } - - void simplifyMathFunctions_fmax() { - // verify fmax(),fmax(),fmaxl() simplifcation - const char code_fmax[] ="void f() {\n" - " std::cout << fmax(-1.0,0);\n" - " std::cout << fmax(1.0,0);\n" - " std::cout << fmax(0,0);\n" - " std::cout << fmaxf(-1.0,0);\n" - " std::cout << fmaxf(1.0,0);\n" - " std::cout << fmaxf(0,0);\n" - " std::cout << fmaxl(-1.0,0);\n" - " std::cout << fmaxl(1.0,0);\n" - " std::cout << fmaxl(0,0);\n" - "}"; - - const char expected_fmax[] = "void f ( ) {\n" - "std :: cout << 0 ;\n" - "std :: cout << 1.0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1.0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 0 ;\n" - "std :: cout << 1.0 ;\n" - "std :: cout << 0 ;\n" - "}"; - ASSERT_EQUALS(expected_fmax, tokWithNewlines(code_fmax)); - - // do not simplify this case - const char code_fmax1[] = "float f(float f) { return fmax(f,0);}"; - const char expected_fmax1[] = "float f ( float f ) { return fmax ( f , 0 ) ; }"; - ASSERT_EQUALS(expected_fmax1, tokWithNewlines(code_fmax1)); - } - - void simplifyMathExpressions() { //#1620 - const char code1[] = "void foo() {\n" - " std::cout<> y ;", tok("; x >>= y;")); - - ASSERT_EQUALS("{ x = x + y ; }", tok("{ x += y;}")); - ASSERT_EQUALS("{ x = x - y ; }", tok("{ x -= y;}")); - ASSERT_EQUALS("{ x = x * y ; }", tok("{ x *= y;}")); - ASSERT_EQUALS("{ x = x / y ; }", tok("{ x /= y;}")); - ASSERT_EQUALS("{ x = x % y ; }", tok("{ x %= y;}")); - ASSERT_EQUALS("{ x = x & y ; }", tok("{ x &= y;}")); - ASSERT_EQUALS("{ x = x | y ; }", tok("{ x |= y;}")); - ASSERT_EQUALS("{ x = x ^ y ; }", tok("{ x ^= y;}")); - ASSERT_EQUALS("{ x = x << y ; }", tok("{ x <<= y;}")); - ASSERT_EQUALS("{ x = x >> y ; }", tok("{ x >>= y;}")); - - ASSERT_EQUALS("; * p = * p + y ;", tok("; *p += y;")); - ASSERT_EQUALS("; ( * p ) = ( * p ) + y ;", tok("; (*p) += y;")); - ASSERT_EQUALS("; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ;", tok("; *(p[0]) += y;")); - ASSERT_EQUALS("; p [ { 1 , 2 } ] = p [ { 1 , 2 } ] + y ;", tok("; p[{1,2}] += y;")); - - ASSERT_EQUALS("void foo ( ) { switch ( n ) { case 0 : ; x = x + y ; break ; } }", tok("void foo() { switch (n) { case 0: x += y; break; } }")); - - ASSERT_EQUALS("; x . y = x . y + 1 ;", tok("; x.y += 1;")); - - ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tok("; x[0] += 1;")); - ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tok("; x[y-1] += 1;")); - ASSERT_EQUALS("; x [ y ] = x [ y ++ ] + 1 ;", tok("; x[y++] += 1;")); - ASSERT_EQUALS("; x [ ++ y ] = x [ y ] + 1 ;", tok("; x[++y] += 1;")); - - ASSERT_EQUALS(";", tok(";x += 0;")); - TODO_ASSERT_EQUALS(";", "; x = x + '\\0' ;", tok("; x += '\\0'; ")); - ASSERT_EQUALS(";", tok(";x -= 0;")); - ASSERT_EQUALS(";", tok(";x |= 0;")); - ASSERT_EQUALS(";", tok(";x *= 1;")); - ASSERT_EQUALS(";", tok(";x /= 1;")); - - ASSERT_EQUALS("; a . x ( ) = a . x ( ) + 1 ;", tok("; a.x() += 1;")); - ASSERT_EQUALS("; x ( 1 ) = x ( 1 ) + 1 ;", tok("; x(1) += 1;")); - - // #2368 - ASSERT_EQUALS("{ j = j - i ; }", tok("{if (false) {} else { j -= i; }}")); - - // #2714 - wrong simplification of "a += b?c:d;" - ASSERT_EQUALS("; a = a + ( b ? c : d ) ;", tok("; a+=b?c:d;")); - ASSERT_EQUALS("; a = a * ( b + 1 ) ;", tok("; a*=b+1;")); - - ASSERT_EQUALS("; a = a + ( b && c ) ;", tok("; a+=b&&c;")); - ASSERT_EQUALS("; a = a * ( b || c ) ;", tok("; a*=b||c;")); - ASSERT_EQUALS("; a = a | ( b == c ) ;", tok("; a|=b==c;")); - - // #3469 - ASSERT_EQUALS("; a = a + ( b = 1 ) ;", tok("; a += b = 1;")); - - // #7571 - ASSERT_EQUALS("; foo = foo + [ & ] ( ) { } ;", tok("; foo += [&]() {int i;};")); - - // #8796 - ASSERT_EQUALS("{ return ( a = b ) += c ; }", tok("{ return (a = b) += c; }")); - } - - void combine_strings() { const char code1[] = "void foo()\n"