1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "checkunusedfunctions.h"
#include "errortypes.h"
#include "fixture.h"
#include "helpers.h"
#include "platform.h"
#include "settings.h"
#include "tokenize.h"
#include "tokenlist.h"
#include <cstddef>
#include <sstream>
#include <string>
class TestUnusedFunctions : public TestFixture {
public:
TestUnusedFunctions() : TestFixture("TestUnusedFunctions") {}
private:
const Settings settings = settingsBuilder().severity(Severity::style).build();
void run() override {
TEST_CASE(incondition);
TEST_CASE(return1);
TEST_CASE(return2);
TEST_CASE(return3);
TEST_CASE(callback1);
TEST_CASE(callback2);
TEST_CASE(else1);
TEST_CASE(functionpointer);
TEST_CASE(template1);
TEST_CASE(template2);
TEST_CASE(template3);
TEST_CASE(template4); // #9805
TEST_CASE(template5);
TEST_CASE(template6); // #10475 crash
TEST_CASE(template7); // #9766 crash
TEST_CASE(template8);
TEST_CASE(template9);
TEST_CASE(template10);
TEST_CASE(throwIsNotAFunction);
TEST_CASE(unusedError);
TEST_CASE(unusedMain);
TEST_CASE(initializationIsNotAFunction);
TEST_CASE(operator1); // #3195
TEST_CASE(operator2); // #7974
TEST_CASE(returnRef);
TEST_CASE(attribute); // #3471 - FP __attribute__(constructor)
TEST_CASE(initializer_list);
TEST_CASE(member_function_ternary);
TEST_CASE(boost);
TEST_CASE(enumValues);
TEST_CASE(recursive);
TEST_CASE(multipleFiles); // same function name in multiple files
TEST_CASE(lineNumber); // Ticket 3059
TEST_CASE(ignore_declaration); // ignore declaration
TEST_CASE(operatorOverload);
TEST_CASE(entrypointsWin);
TEST_CASE(entrypointsWinU);
TEST_CASE(entrypointsUnix);
TEST_CASE(includes);
TEST_CASE(virtualFunc);
TEST_CASE(parensInit);
TEST_CASE(typeInCast);
TEST_CASE(attributeCleanup);
TEST_CASE(attributeUnused);
TEST_CASE(attributeMaybeUnused);
TEST_CASE(staticFunction);
}
struct CheckOptions
{
CheckOptions() = default;
Platform::Type platform = Platform::Type::Native;
const Settings* s = nullptr;
bool cpp = true;
};
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).platform(options.platform).build();
// Tokenize..
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);
// Check for unused functions..
CheckUnusedFunctions checkUnusedFunctions;
checkUnusedFunctions.parseTokens(tokenizer, settings1);
(checkUnusedFunctions.check)(settings1, *this); // TODO: check result
}
// TODO: get rid of this
void check_(const char* file, int line, const std::string& code ) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
// Check for unused functions..
CheckUnusedFunctions checkUnusedFunctions;
checkUnusedFunctions.parseTokens(tokenizer, settings);
(checkUnusedFunctions.check)(settings, *this); // TODO: check result
}
void incondition() {
check("int f1()\n"
"{\n"
" if (f1())\n"
" { }\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}
void return1() {
check("int f1()\n"
"{\n"
" return f1();\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}
void return2() {
check("char * foo()\n"
"{\n"
" return foo();\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
}
void return3() {
check("typedef void (*VoidFunc)();\n" // #9602
"void sayHello() {\n"
" printf(\"Hello World\\n\");\n"
"}\n"
"VoidFunc getEventHandler() {\n"
" return sayHello;\n"
"}\n"
"void indirectHello() {\n"
" VoidFunc handler = getEventHandler();\n"
" handler();\n"
"}\n"
"int main() {\n"
" indirectHello();\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void callback1() {
check("void f1()\n"
"{\n"
" void (*f)() = cond ? f1 : NULL;\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}
void callback2() { // #8677
check("class C {\n"
"public:\n"
" void callback();\n"
" void start();\n"
"};\n"
"\n"
"void C::callback() {}\n" // <- not unused
"\n"
"void C::start() { ev.set<C, &C::callback>(this); }");
ASSERT_EQUALS("[test.cpp:9]: (style) The function 'start' is never used.\n", errout_str());
}
void else1() {
check("void f1()\n"
"{\n"
" if (cond) ;\n"
" else f1();\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}
void functionpointer() {
check("void foo() { }\n"
"int main() {\n"
" f(&foo);\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo() { }\n"
"int main() {\n"
" f(&::foo);\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("namespace abc {\n"
" void foo() { }\n"
"};\n"
"int main() {\n"
" f(&abc::foo);\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("namespace abc {\n"
" void foo() { }\n"
"};\n"
"int main() {\n"
" f = &abc::foo;\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("namespace abc {\n"
" void foo() { }\n"
"};\n"
"int main() {\n"
" f = &::abc::foo;\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("namespace abc {\n" // #3875
" void foo() { }\n"
"};\n"
"int main() {\n"
" f(abc::foo);\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
check("template <class T> T f()\n" // #13117
"{\n"
" return T(1);\n"
"}\n"
"int main(void)\n"
"{\n"
" auto *ptr = &f<int>;\n"
" return ptr();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void template1() {
check("template<class T> void foo() { }\n"
"\n"
"int main()\n"
"{\n"
" foo<int>();\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void template2() {
check("void f() { }\n"
"\n"
"template<class T> void g()\n"
"{\n"
" f();\n"
"}\n"
"\n"
"void h() { g<int>(); h(); }");
ASSERT_EQUALS("[test.cpp:8]: (style) The function 'h' is never used.\n", errout_str());
}
void template3() { // #4701
check("class X {\n"
"public:\n"
" void bar() { foo<int>(0); }\n"
"private:\n"
" template<typename T> void foo( T t ) const;\n"
"};\n"
"template<typename T> void X::foo( T t ) const { }");
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'bar' is never used.\n", errout_str());
}
void template4() { // #9805
check("struct A {\n"
" int a = 0;\n"
" void f() { a = 1; }\n"
" template <typename T, typename... Args> auto call(const Args &... args) -> T {\n"
" a = 2;\n"
" return T{};\n"
" }\n"
"};\n"
"\n"
"struct B : public A {\n"
" void test() {\n"
" f();\n"
" call<int>(1, 2, 3);\n"
" test();\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:11]: (style) The function 'test' is never used.\n", errout_str());
}
void template5() { // #9220
check("void f(){}\n"
"\n"
"typedef void(*Filter)();\n"
"\n"
"template <Filter fun>\n"
"void g() { fun(); }\n"
"\n"
"int main() { g<f>(); return 0;}");
ASSERT_EQUALS("", errout_str());
}
void template6() { // #10475
check("template<template<typename...> class Ref, typename... Args>\n"
"struct Foo<Ref<Args...>, Ref> : std::true_type {};\n");
ASSERT_EQUALS("", errout_str());
}
void template7()
{ // #9766
check("void f() {\n"
" std::array<std::array<double,3>,3> array;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f' is never used.\n", errout_str());
}
void template8() { // #11485
check("struct S {\n"
" template<typename T>\n"
" void tf(const T&) { }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'tf' is never used.\n", errout_str());
check("struct C {\n"
" template<typename T>\n"
" void tf(const T&) { }\n"
"};\n"
"int main() {\n"
" C c;\n"
" c.tf(1.5);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("struct C {\n"
" template<typename T>\n"
" void tf(const T&) { }\n"
"};\n"
"int main() {\n"
" C c;\n"
" c.tf<int>(1);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void template9() {
check("template<class T>\n" // #7739
"void f(T const& t) {}\n"
"template<class T>\n"
"void g(T const& t) {\n"
" f(t);\n"
"}\n"
"template<>\n"
"void f<double>(double const& d) {}\n"
"int main() {\n"
" g(2);\n"
" g(3.14);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("template <typename T> T f(T);\n" // #9222
"template <typename T> T f(T i) { return i; }\n"
"template int f<int>(int);\n"
"int main() { return f(int(2)); }\n");
ASSERT_EQUALS("", errout_str());
}
void template10() {
check("template<typename T>\n" // #12013, don't crash
"struct S {\n"
" static const int digits = std::numeric_limits<T>::digits;\n"
" using type = std::conditional<digits < 32, std::int32_t, std::int64_t>::type;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void throwIsNotAFunction() {
check("struct A {void f() const throw () {}}; int main() {A a; a.f();}");
ASSERT_EQUALS("", errout_str());
}
void unusedError() {
check("void foo() {}\n"
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
check("void foo() const {}\n"
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
check("void foo() const throw() {}\n"
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
check("void foo() throw() {}\n"
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
}
void unusedMain() {
check("int main() { }");
ASSERT_EQUALS("", errout_str());
}
void initializationIsNotAFunction() {
check("struct B: N::A {\n"
" B(): N::A() {};\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void operator1() {
check("struct Foo { void operator()(int a) {} };");
ASSERT_EQUALS("", errout_str());
check("struct Foo { operator std::string(int a) {} };");
ASSERT_EQUALS("", errout_str());
}
void operator2() { // #7974
check("bool operator==(const data_t& a, const data_t& b) {\n"
" return (a.fd == b.fd);\n"
"}\n"
"bool operator==(const event& a, const event& b) {\n"
" return ((a.events == b.events) && (a.data == b.data));\n"
"}\n"
"int main(event a, event b) {\n"
" return a == b;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void returnRef() {
check("int& foo() {return x;}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
}
void attribute() { // #3471 - FP __attribute__((constructor))
check("void __attribute__((constructor)) f() {}");
ASSERT_EQUALS("", errout_str());
check("void __attribute__((constructor(1000))) f() {}");
ASSERT_EQUALS("", errout_str());
check("void __attribute__((destructor)) f() {}");
ASSERT_EQUALS("", errout_str());
check("void __attribute__((destructor(1000))) f() {}");
ASSERT_EQUALS("", errout_str());
// alternate syntax
check("__attribute__((constructor)) void f() {}");
ASSERT_EQUALS("", errout_str());
check("__attribute__((constructor(1000))) void f() {}");
ASSERT_EQUALS("", errout_str());
check("__attribute__((destructor)) void f() {}");
ASSERT_EQUALS("", errout_str());
check("__attribute__((destructor(1000))) void f() {}");
ASSERT_EQUALS("", errout_str());
// alternate syntax
check("void f() __attribute__((constructor));\n"
"void f() { }");
ASSERT_EQUALS("", errout_str());
check("void f() __attribute__((constructor(1000)));\n"
"void f() { }");
ASSERT_EQUALS("", errout_str());
check("void f() __attribute__((destructor));\n"
"void f() { }");
ASSERT_EQUALS("", errout_str());
check("void f() __attribute__((destructor(1000)));\n"
"void f() { }");
ASSERT_EQUALS("", errout_str());
// Don't crash on wrong syntax
check("int x __attribute__((constructor));\n"
"int y __attribute__((destructor));");
// #10661
check("extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) { return 0; }\n");
ASSERT_EQUALS("", errout_str());
check("[[maybe_unused]] void f() {}\n" // #13268
"__attribute__((unused)) void g() {}\n");
ASSERT_EQUALS("", errout_str());
}
void initializer_list() {
check("int foo() { return 0; }\n"
"struct A {\n"
" A() : m_i(foo())\n"
" {}\n"
"int m_i;\n"
"};");
ASSERT_EQUALS("", errout_str());
// #8580
check("int foo() { return 12345; }\n"
"int bar(std::function<int()> func) { return func(); }\n"
"\n"
"class A {\n"
"public:\n"
" A() : a(bar([] { return foo(); })) {}\n"
" const int a;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void member_function_ternary() {
check("struct Foo {\n"
" void F1() {}\n"
" void F2() {}\n"
"};\n"
"int main(int argc, char *argv[]) {\n"
" Foo foo;\n"
" void (Foo::*ptr)();\n"
" ptr = (argc > 1 && !strcmp(argv[1], \"F2\")) ? &Foo::F2 : &Foo::F1;\n"
" (foo.*ptr)();\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void boost() {
check("static void _xy(const char *b, const char *e) {}\n"
"void f() {\n"
" parse(line, blanks_p >> ident[&_xy] >> blanks_p >> eol_p).full;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'f' is never used.\n", errout_str());
}
void enumValues() { // #11486
check("enum E1 { Break1 };\n"
"struct S {\n"
" enum class E { Break };\n"
" void Break() {}\n"
" void Break1() {}\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Break' is never used.\n"
"[test.cpp:5]: (style) The function 'Break1' is never used.\n",
errout_str());
check("struct S {\n" // #12899
" void f() {}\n"
"};\n"
"enum E { f };\n"
"int main() {\n"
" E e{ f };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'f' is never used.\n", errout_str());
}
void recursive() {
check("void f() {\n" // #8159
" f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f' is never used.\n",
errout_str());
}
void multipleFiles() {
CheckUnusedFunctions c;
const char code[] = "static void f() { }";
for (int i = 1; i <= 2; ++i) {
const std::string fname = "test" + std::to_string(i) + ".cpp";
Tokenizer tokenizer(settings, *this);
std::istringstream istr(code);
ASSERT(tokenizer.list.createTokens(istr, fname));
ASSERT(tokenizer.simplifyTokens1(""));
c.parseTokens(tokenizer, settings);
}
// Check for unused functions..
(c.check)(settings, *this); // TODO: check result
ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout_str());
}
void lineNumber() {
check("void foo();\n"
"void bar() {}\n"
"int main() {}");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used.\n", errout_str());
}
void ignore_declaration() {
check("void f();\n"
"void f() {}");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'f' is never used.\n", errout_str());
check("void f(void) {}\n"
"void (*list[])(void) = {f};");
ASSERT_EQUALS("", errout_str());
}
void operatorOverload() {
check("class A {\n"
"private:\n"
" friend std::ostream & operator<<(std::ostream &, const A&);\n"
"};\n"
"std::ostream & operator<<(std::ostream &os, const A&) {\n"
" os << \"This is class A\";\n"
"}");
ASSERT_EQUALS("", errout_str());
check("class A{};\n"
"A operator + (const A &, const A &){ return A(); }\n"
"A operator - (const A &, const A &){ return A(); }\n"
"A operator * (const A &, const A &){ return A(); }\n"
"A operator / (const A &, const A &){ return A(); }\n"
"A operator % (const A &, const A &){ return A(); }\n"
"A operator & (const A &, const A &){ return A(); }\n"
"A operator | (const A &, const A &){ return A(); }\n"
"A operator ~ (const A &){ return A(); }\n"
"A operator ! (const A &){ return A(); }\n"
"bool operator < (const A &, const A &){ return true; }\n"
"bool operator > (const A &, const A &){ return true; }\n"
"A operator += (const A &, const A &){ return A(); }\n"
"A operator -= (const A &, const A &){ return A(); }\n"
"A operator *= (const A &, const A &){ return A(); }\n"
"A operator /= (const A &, const A &){ return A(); }\n"
"A operator %= (const A &, const A &){ return A(); }\n"
"A operator &= (const A &, const A &){ return A(); }\n"
"A operator ^= (const A &, const A &){ return A(); }\n"
"A operator |= (const A &, const A &){ return A(); }\n"
"A operator << (const A &, const int){ return A(); }\n"
"A operator >> (const A &, const int){ return A(); }\n"
"A operator <<= (const A &, const int){ return A(); }\n"
"A operator >>= (const A &, const int){ return A(); }\n"
"bool operator == (const A &, const A &){ return true; }\n"
"bool operator != (const A &, const A &){ return true; }\n"
"bool operator <= (const A &, const A &){ return true; }\n"
"bool operator >= (const A &, const A &){ return true; }\n"
"A operator && (const A &, const int){ return A(); }\n"
"A operator || (const A &, const int){ return A(); }\n"
"A operator ++ (const A &, const int){ return A(); }\n"
"A operator ++ (const A &){ return A(); }\n"
"A operator -- (const A &, const int){ return A(); }\n"
"A operator -- (const A &){ return A(); }\n"
"A operator , (const A &, const A &){ return A(); }");
ASSERT_EQUALS("", errout_str());
check("class A {\n"
"public:\n"
" static void * operator new(std::size_t);\n"
" static void * operator new[](std::size_t);\n"
"};\n"
"void * A::operator new(std::size_t s) {\n"
" return malloc(s);\n"
"}\n"
"void * A::operator new[](std::size_t s) {\n"
" return malloc(s);\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void entrypointsWin() {
check("int WinMain() { }");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'WinMain' is never used.\n", errout_str());
check("int _tmain() { }");
ASSERT_EQUALS("[test.cpp:1]: (style) The function '_tmain' is never used.\n", errout_str());
const Settings s = settingsBuilder(settings).library("windows.cfg").build();
check("int WinMain() { }", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("int _tmain() { }", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
}
void entrypointsWinU() {
check("int wWinMain() { }");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'wWinMain' is never used.\n", errout_str());
check("int _tmain() { }");
ASSERT_EQUALS("[test.cpp:1]: (style) The function '_tmain' is never used.\n", errout_str());
const Settings s = settingsBuilder(settings).library("windows.cfg").build();
check("int wWinMain() { }", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("int _tmain() { }", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
}
void entrypointsUnix() {
check("int _init() { }\n"
"int _fini() { }\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function '_init' is never used.\n"
"[test.cpp:2]: (style) The function '_fini' is never used.\n", errout_str());
const Settings s = settingsBuilder(settings).library("gnu.cfg").build();
check("int _init() { }\n"
"int _fini() { }\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
}
// TODO: fails because the location information is not be preserved by PreprocessorHelper::getcode()
void includes()
{
// #11483
const char inc[] = "class A {\n"
"public:\n"
" void f() {}\n"
"};";
const char code[] = R"(#include "test.h")";
ScopedFile header("test.h", inc);
const std::string processed = PreprocessorHelper::getcode(settings, *this, code, "", "test.cpp");
check(processed);
TODO_ASSERT_EQUALS("[test.h:3]: (style) The function 'f' is never used.\n", "[test.cpp:3]: (style) The function 'f' is never used.\n", errout_str());
}
void virtualFunc()
{
check("struct D : public B {\n" // #10660
" virtual void f() {}\n"
" void g() override {}\n"
" void h() final {}\n"
"};\n");
ASSERT_EQUALS("", errout_str());
check("struct B {\n"
" virtual void f() = 0;\n"
" void g();\n"
"};\n"
"struct D : B {\n"
" void f() override {}\n"
"};\n"
"int main() {\n"
" D d;\n"
" d.g();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void parensInit()
{
check("struct S {\n" // #12898
" void url() {}\n"
"};\n"
"int main() {\n"
" const int url(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'url' is never used.\n", errout_str());
}
void typeInCast()
{
check("struct S {\n" // #12901
" void Type() {}\n"
"};\n"
"int main() {\n"
" struct Type {} t;\n"
" Type t2{ (Type)t };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'Type' is never used.\n", errout_str());
}
void attributeCleanup()
{
check("void clean(void *ptr) {}\n"
"int main() {\n"
" void * __attribute__((cleanup(clean))) p;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void attributeUnused()
{
check("[[unused]] void f() {}\n");
ASSERT_EQUALS("", errout_str());
check("[[gnu::unused]] void f() {}\n");
ASSERT_EQUALS("", errout_str());
check("__attribute__((unused)) void f() {}\n");
ASSERT_EQUALS("", errout_str());
}
void attributeMaybeUnused()
{
check("[[__maybe_unused__]] void f() {}\n", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("", errout_str());
check("[[maybe_unused]] void f() {}\n", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("", errout_str());
check("[[maybe_unused]] void f() {}\n");
ASSERT_EQUALS("", errout_str());
}
void staticFunction()
{
check("void f(void) {}\n"
"int main() {\n"
" f();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(void) {}\n"
"int main() {\n"
" f();\n"
"}\n", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("[test.c:1]: (style) The function 'f' should have static linkage since it is not used outside of its translation unit.\n", errout_str());
}
};
REGISTER_TEST(TestUnusedFunctions)
|