Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEMPLATE_TEST_CASE triggers -Wstring-plus-int #1806

Closed
michaelvlach opened this issue Nov 17, 2019 · 3 comments
Closed

TEMPLATE_TEST_CASE triggers -Wstring-plus-int #1806

michaelvlach opened this issue Nov 17, 2019 · 3 comments

Comments

@michaelvlach
Copy link
Contributor

michaelvlach commented Nov 17, 2019

Describe the bug
Clang and clang-tidy warns with -Wstring-plus-int whenever TEMPLATE_TEST_CASE is used. It triggers with any template argument (not just int).

The warning message:

adding 'int' to a string doees not append to the string

Suggested fix:

use array indexing to silence this warning

Expected behavior
TEMPLATE_TEST_CASE does not generate -Wstring-plus-int warning.

Reproduction steps

  1. Use clang or clang-tidy.
  2. Compile either TEMPLATE_TEST_CASE example -OR- following minimal:
TEMPLATE_TEST_CASE("", "", int)
{
}

Platform information:

  • OS: Windows 10
  • Compiler+version: clang9, clang-tidy9
  • Catch version: v1.2.10

Additional context
I cannot pinpoint the exact location in the code that triggers this because the warning stack exceeds its limits for macro expansions but I reckon people familiar with the Catch2 internals would have a good idea what might be the issue that triggers this.

It will be something like "mystring" + 1 or somesuch. And the fix is to advance address of the string, not the string itself, e.g. &"mystring"[1].

See live demo: https://godbolt.org/z/TkaTSV

@horenmar
Copy link
Member

Can't reproduce with Clang

$ cat 1806.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEMPLATE_TEST_CASE("", "", int) {}
$ clang++ --version
clang version 10.0.0-svn375026-1~exp1+0~20191016181515.2549~1.gbp364367 (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ clang++ -std=c++11 1806.cpp -Wstring-plus-int
$

@michaelvlach
Copy link
Contributor Author

michaelvlach commented Nov 17, 2019

I cannot reproduce on Ubuntu 19.04 with clang++-9 or with clang-tidy-9 either but on Windows 10 with LLVM 9.0 installed I can reproduce it:

C:\dev\projects\ADev\tools>clang++ --version
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

Then compilation (of the same file as you did):

C:\dev\projects\ADev\tools>clang++ test.cpp
test.cpp:4:1: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
TEMPLATE_TEST_CASE("", "", int) {}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Catch2/single_include/catch2/catch.hpp:17356:64: note: expanded from macro 'TEMPLATE_TEST_CASE'
#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) )
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Catch2/single_include/catch2/catch.hpp:1098:38: note: expanded from macro 'INTERNAL_CATCH_TEMPLATE_TEST_CASE'
        INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_...
                                     ^
./Catch2/single_include/catch2/catch.hpp:1079:74: note: expanded from macro 'INTERNAL_CATCH_TEMPLATE_TEST_CASE_2'
                    constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS...
                                                                         ^
note: (skipping 2189 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
./Catch2/single_include/catch2/catch.hpp:670:37: note: expanded from macro 'CATCH_RECURSION_LEVEL0'
#define CATCH_RECURSION_LEVEL0(...) __VA_ARGS__
                                    ^
./Catch2/single_include/catch2/catch.hpp:678:42: note: expanded from macro 'INTERNAL_CATCH_EXPAND_VARGS'
#define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__
                                         ^
./Catch2/single_include/catch2/catch.hpp:678:42: note: expanded from macro 'INTERNAL_CATCH_EXPAND_VARGS'
#define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__
                                         ^~~~~~~~~~~
test.cpp:4:1: note: use array indexing to silence this warning
./Catch2/single_include/catch2/catch.hpp:17356:64: note: expanded from macro 'TEMPLATE_TEST_CASE'
#define TEMPLATE_TEST_CASE( ... ) INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE( __VA_ARGS__ ) )
                                                               ^
./Catch2/single_include/catch2/catch.hpp:1098:38: note: expanded from macro 'INTERNAL_CATCH_TEMPLATE_TEST_CASE'
        INTERNAL_CATCH_EXPAND_VARGS( INTERNAL_CATCH_TEMPLATE_TEST_CASE_2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_...
                                     ^
./Catch2/single_include/catch2/catch.hpp:1079:74: note: expanded from macro 'INTERNAL_CATCH_TEMPLATE_TEST_CASE_2'
                    constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS...

The clang-tidy is the same story, only on Windows 10 it produces this same warning (among others).

@horenmar
Copy link
Member

Right, it seems that Clang actually masks itself for MSVC well enough to get the path for MSVC's old preprocessor, which contains this

#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) (INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) + 1)

because the preprocessor just prepends a space when expanding the argument with #__VA_ARGS__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants