@@ -1901,7 +1901,7 @@ class TestOther : public TestFixture {
19011901 template<size_t size>
19021902 void checkOldStylePointerCast_(const char* file, int line, const char (&code)[size], Standards::cppstd_t std = Standards::CPPLatest) {
19031903
1904- const Settings settings = settingsBuilder().severity(Severity::style).cpp(std).build();
1904+ const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::portability). cpp(std).build();
19051905
19061906 // Tokenize..
19071907 SimpleTokenizer tokenizerCpp(settings, *this);
@@ -1912,75 +1912,44 @@ class TestOther : public TestFixture {
19121912 }
19131913
19141914 void oldStylePointerCast() {
1915- checkOldStylePointerCast("class Base;\n"
1916- "void foo()\n"
1915+ checkOldStylePointerCast("class Base{};\n"
1916+ "class Derived: public Base {};\n"
1917+ "void foo(Derived* derived)\n"
19171918 "{\n"
19181919 " Base * b = (Base *) derived;\n"
19191920 "}");
1920- ASSERT_EQUALS("[test.cpp:4 :16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1921+ ASSERT_EQUALS("[test.cpp:5 :16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
19211922
1922- checkOldStylePointerCast("class Base;\n"
1923- "void foo()\n"
1923+ checkOldStylePointerCast("class Base{};\n"
1924+ "class Derived: public Base {};\n"
1925+ "void foo(Derived* derived)\n"
19241926 "{\n"
19251927 " Base * b = (const Base *) derived;\n"
19261928 "}");
1927- ASSERT_EQUALS("[test.cpp:4:17]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1928-
1929- checkOldStylePointerCast("class Base;\n"
1930- "void foo()\n"
1931- "{\n"
1932- " Base * b = (const Base * const) derived;\n"
1933- "}");
1934- ASSERT_EQUALS("[test.cpp:4:23]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1935-
1936- checkOldStylePointerCast("class Base;\n"
1937- "void foo()\n"
1938- "{\n"
1939- " Base * b = (volatile Base *) derived;\n"
1940- "}");
1941- ASSERT_EQUALS("[test.cpp:4:17]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1942-
1943- checkOldStylePointerCast("class Base;\n"
1944- "void foo()\n"
1945- "{\n"
1946- " Base * b = (volatile Base * const) derived;\n"
1947- "}");
1948- ASSERT_EQUALS("[test.cpp:4:26]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1949-
1950- checkOldStylePointerCast("class Base;\n"
1951- "void foo()\n"
1952- "{\n"
1953- " Base * b = (const volatile Base *) derived;\n"
1954- "}");
1955- ASSERT_EQUALS("[test.cpp:4:23]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1929+ ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
19561930
1957- checkOldStylePointerCast("class Base;\n"
1958- "void foo()\n"
1959- "{\n"
1960- " Base * b = (const volatile Base * const) derived;\n"
1961- "}");
1962- ASSERT_EQUALS("[test.cpp:4:32]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1963-
1964- checkOldStylePointerCast("class Base;\n"
1931+ checkOldStylePointerCast("class Base{};\n"
1932+ "class Derived: public Base {};\n"
19651933 "void foo()\n"
19661934 "{\n"
1967- " Base * b = (const Base *) ( new Derived() );\n"
1935+ " Base * b = (Base *) ( new Derived() );\n"
19681936 "}");
1969- ASSERT_EQUALS("[test.cpp:4:17 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1937+ ASSERT_EQUALS("[test.cpp:5:16 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
19701938
1971- checkOldStylePointerCast("class Base;\n"
1939+ checkOldStylePointerCast("class Base{};\n"
1940+ "class Derived: public Base {};\n"
19721941 "void foo()\n"
19731942 "{\n"
1974- " Base * b = (const Base *) new Derived();\n"
1943+ " Base * b = (Base *) new Derived();\n"
19751944 "}");
1976- ASSERT_EQUALS("[test.cpp:4:17 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1945+ ASSERT_EQUALS("[test.cpp:5:16 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
19771946
1978- checkOldStylePointerCast("class Base;\n"
1947+ checkOldStylePointerCast("class Base{} ;\n"
19791948 "void foo()\n"
19801949 "{\n"
1981- " Base * b = (const Base *) new short[10];\n"
1950+ " Base * b = (Base *) new short[10];\n"
19821951 "}");
1983- ASSERT_EQUALS("[test.cpp:4:17 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1952+ ASSERT_EQUALS("[test.cpp:4:16 ]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
19841953
19851954 checkOldStylePointerCast("class B;\n"
19861955 "class A\n"
@@ -1997,17 +1966,23 @@ class TestOther : public TestFixture {
19971966 ASSERT_EQUALS("", errout_str());
19981967
19991968 // #3630
2000- checkOldStylePointerCast("class SomeType;\n"
1969+ checkOldStylePointerCast("class SomeType{} ;\n"
20011970 "class X : public Base {\n"
20021971 " X() : Base((SomeType*)7) {}\n"
20031972 "};");
2004- ASSERT_EQUALS("[test.cpp:3:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
1973+ ASSERT_EQUALS("[test.cpp:3:16]: (portability) Casting non-zero integer literal in decimal or octal format to pointer. [intToPointerCast]\n", errout_str());
1974+
1975+ checkOldStylePointerCast("class SomeType{};\n"
1976+ "class X : public Base {\n"
1977+ " X() : Base((SomeType*)0x7000) {}\n" // <- it's common in embedded code to cast address
1978+ "};");
1979+ ASSERT_EQUALS("", errout_str());
20051980
20061981 checkOldStylePointerCast("class SomeType;\n"
20071982 "class X : public Base {\n"
20081983 " X() : Base((SomeType*)var) {}\n"
20091984 "};");
2010- ASSERT_EQUALS("[test.cpp:3:16]: (style) C-style pointer casting [cstyleCast]\n ", errout_str());
1985+ ASSERT_EQUALS("", errout_str());
20111986
20121987 checkOldStylePointerCast("class SomeType;\n"
20131988 "class X : public Base {\n"
@@ -2032,7 +2007,7 @@ class TestOther : public TestFixture {
20322007 " std::vector<Base*> v;\n"
20332008 " v.push_back((Base*)new Derived);\n"
20342009 "}");
2035- ASSERT_EQUALS("[test.cpp:5:15]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
2010+ // FIXME ASSERT_EQUALS("[test.cpp:5:15]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
20362011
20372012 // #7709
20382013 checkOldStylePointerCast("typedef struct S S;\n"
@@ -2055,27 +2030,38 @@ class TestOther : public TestFixture {
20552030 " TT* tt = (TT*)i;\n"
20562031 " TT2* tt2 = (TT2*)i;\n"
20572032 "}\n");
2058- ASSERT_EQUALS("[test.cpp:10:13 ]: (style) C-style pointer casting [cstyleCast]\n"
2059- "[test.cpp:11:15 ]: (style) C-style pointer casting [cstyleCast]\n"
2060- "[test.cpp:12:22 ]: (style) C-style pointer casting [cstyleCast]\n"
2061- "[test.cpp:13:13 ]: (style) C-style pointer casting [cstyleCast]\n"
2062- "[test.cpp:14:21 ]: (style) C-style pointer casting [cstyleCast]\n"
2033+ ASSERT_EQUALS("[test.cpp:10:12 ]: (style) C-style pointer casting [cstyleCast]\n"
2034+ "[test.cpp:11:14 ]: (style) C-style pointer casting [cstyleCast]\n"
2035+ "[test.cpp:12:21 ]: (style) C-style pointer casting [cstyleCast]\n"
2036+ "[test.cpp:13:12 ]: (style) C-style pointer casting [cstyleCast]\n"
2037+ "[test.cpp:14:20 ]: (style) C-style pointer casting [cstyleCast]\n"
20632038 "[test.cpp:15:15]: (style) C-style pointer casting [cstyleCast]\n"
20642039 "[test.cpp:16:16]: (style) C-style pointer casting [cstyleCast]\n"
20652040 "[test.cpp:17:16]: (style) C-style pointer casting [cstyleCast]\n"
2066- "[test.cpp:18:15 ]: (style) C-style pointer casting [cstyleCast]\n"
2067- "[test.cpp:19:17 ]: (style) C-style pointer casting [cstyleCast]\n",
2041+ "[test.cpp:18:14 ]: (style) C-style pointer casting [cstyleCast]\n"
2042+ "[test.cpp:19:16 ]: (style) C-style pointer casting [cstyleCast]\n",
20682043 errout_str());
20692044
20702045 // #8649
20712046 checkOldStylePointerCast("struct S {};\n"
20722047 "void g(S*& s);\n"
2073- "void f(int i) {\n"
2048+ "void f(uintptr_t i) {\n"
20742049 " g((S*&)i);\n"
20752050 " S*& r = (S*&)i;\n"
20762051 "}\n");
2077- ASSERT_EQUALS("[test.cpp:4:7]: (style) C-style pointer casting [cstyleCast]\n"
2078- "[test.cpp:5:13]: (style) C-style pointer casting [cstyleCast]\n",
2052+ ASSERT_EQUALS("",
2053+ errout_str());
2054+
2055+ checkOldStylePointerCast("struct S {};\n"
2056+ "void g(S*& s);\n"
2057+ "void f(uint8_t i) {\n"
2058+ " g((S*&)i);\n"
2059+ " S*& r = (S*&)i;\n"
2060+ "}\n");
2061+ // TODO: these conversions are dangerous, but a separate checker would make sense which
2062+ // also checks C code.
2063+ // clang says: 1.cpp:5:18: warning: cast to 'unsigned char *' from smaller integer type 'uint8_t' (aka 'unsigned char') [-Wint-to-pointer-cast]
2064+ TODO_ASSERT_EQUALS("bad cast from uint8_t to pointer", "",
20792065 errout_str());
20802066
20812067 // #10823
@@ -2085,12 +2071,14 @@ class TestOther : public TestFixture {
20852071 ASSERT_EQUALS("", errout_str());
20862072
20872073 // #5210
2088- checkOldStylePointerCast("void f(void* v1, void* v2) {\n"
2089- " T** p1 = (T**)v1;\n"
2090- " T*** p2 = (T***)v2;\n"
2074+ checkOldStylePointerCast("class Base {};\n"
2075+ "class Derived: public Base {};\n"
2076+ "void f(Derived** d1, Derived*** d2) {\n"
2077+ " Base** p1 = (Base**)d1;\n"
2078+ " Base*** p2 = (Base***)d2;\n"
20912079 "}\n");
2092- ASSERT_EQUALS("[test.cpp:2:14 ]: (style) C-style pointer casting [cstyleCast]\n"
2093- "[test.cpp:3:15 ]: (style) C-style pointer casting [cstyleCast]\n",
2080+ ASSERT_EQUALS("[test.cpp:4:17 ]: (style) C-style pointer casting [cstyleCast]\n"
2081+ "[test.cpp:5:18 ]: (style) C-style pointer casting [cstyleCast]\n",
20942082 errout_str());
20952083
20962084 // #12446
@@ -2104,17 +2092,15 @@ class TestOther : public TestFixture {
21042092 " auto pu = (union U*)p;\n"
21052093 " auto pv = (std::vector<int>*)(p);\n"
21062094 "}\n");
2107- ASSERT_EQUALS("[test.cpp:7:15]: (style) C-style pointer casting [cstyleCast]\n"
2108- "[test.cpp:8:16]: (style) C-style pointer casting [cstyleCast]\n"
2109- "[test.cpp:9:15]: (style) C-style pointer casting [cstyleCast]\n",
2110- errout_str());
2095+ ASSERT_EQUALS("", errout_str()); // There are other checkers and compiler warnings that warn about all C style casts
21112096
21122097 // #12447
2113- checkOldStylePointerCast("void f(const int& i) {\n"
2114- " int& r = (int&)i;\n"
2115- " r = 0;\n"
2098+ checkOldStylePointerCast("class Base {};\n"
2099+ "class Derived: public Base {};\n"
2100+ "void f(const Derived& derived) {\n"
2101+ " Base& b = (Base&)derived;\n"
21162102 "}\n");
2117- ASSERT_EQUALS("[test.cpp:2:12 ]: (style) C-style reference casting [cstyleCast]\n", errout_str());
2103+ ASSERT_EQUALS("[test.cpp:4:13 ]: (style) C-style reference casting [cstyleCast]\n", errout_str());
21182104
21192105 // #11430
21202106 checkOldStylePointerCast("struct B {\n"
@@ -2125,9 +2111,9 @@ class TestOther : public TestFixture {
21252111 "}\n"
21262112 "bool g(B& b) {\n"
21272113 " using float_ptr = float*;\n"
2128- " return N::f(float_ptr(b.data()));\n"
2114+ " return N::f(float_ptr(b.data()));\n" // <- the cast is safe
21292115 "}\n");
2130- ASSERT_EQUALS("[test.cpp:9:17]: (style) C-style pointer casting [cstyleCast]\n ", errout_str());
2116+ ASSERT_EQUALS("", errout_str());
21312117 }
21322118
21332119#define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__)
0 commit comments