This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Emit -Winteger-overflow for arguments in function calls, ObjC messages.
ClosedPublic

Authored by vsapsai on Feb 5 2018, 4:38 PM.

Event Timeline

vsapsai created this revision.Feb 5 2018, 4:38 PM
vsapsai updated this revision to Diff 132917.Feb 5 2018, 4:53 PM
  • Improve tests: check when function argument is a function call itself.

Ping. It would be helpful to know how reasonable is the idea to handle more expression types in Sema::CheckForIntOverflow.

Maybe a stupid idea but in case it makes sense to add these expression types could we also add integer template arguments?

Maybe a stupid idea but in case it makes sense to add these expression types could we also add integer template arguments?

The idea isn't stupid but I think it is already handled. For

template<int N> void f() {}
template<int N = 1000000 * 10240> void g() {}

void foo() {
    f<1000000 * 10240>();
    g();
}

I got errors

template-args.cpp:2:18: error: non-type template argument is not a constant expression
template<int N = 1000000 * 10240> void g() {}
                 ^~~~~~~~~~~~~~~
template-args.cpp:2:26: note: value 10240000000 is outside the range of representable values of type 'int'
template<int N = 1000000 * 10240> void g() {}
                         ^
template-args.cpp:5:5: error: no matching function for call to 'f'
    f<1000000 * 10240>();
    ^~~~~~~~~~~~~~~~~~
template-args.cpp:1:22: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'N'
template<int N> void f() {}
                     ^
template-args.cpp:6:5: error: no matching function for call to 'g'
    g();
    ^
template-args.cpp:2:40: note: candidate template ignored: couldn't infer template argument 'N'
template<int N = 1000000 * 10240> void g() {}
                                       ^
3 errors generated.

Do you have some other template arguments usage in mind?

Sorry, my bad, I tried just older clang version which didn't produce any error.

With reasonably up to date clang I get these:

> cat tmpl_overflow.hpp
template<int N> struct foo { short a; };

template<> struct foo<1000000000000000000 * 10240> { bool a; };

int main() {
    foo<1000000000000000000 * 10240> a;
}
> ~/src/oss/llvm@master/build/bin/clang++ tmpl_overflow.hpp
tmpl_overflow.hpp:3:23: error: non-type template argument is not a constant expression
template<> struct foo<1000000000000000000 * 10240> { bool a; };
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
tmpl_overflow.hpp:3:43: note: value 10240000000000000000000 is outside the range of representable values of type 'long'
template<> struct foo<1000000000000000000 * 10240> { bool a; };
                                          ^
tmpl_overflow.hpp:6:9: error: non-type template argument is not a constant expression
    foo<1000000000000000000 * 10240> a;
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
tmpl_overflow.hpp:6:29: note: value 10240000000000000000000 is outside the range of representable values of type 'long'
    foo<1000000000000000000 * 10240> a;
                            ^
2 errors generated.

I am not actually sure those error messages are correct (not constant? type 'long'?) but I am not sure it's still within the scope of your patch either.

What is interesting is that by using lower value (which is still to big for short) I probably hit different diagnostic check and get different error:

> cat tmpl_overflow.hpp
template<int N> struct foo { short a; };

template<> struct foo<100000000000000 * 10240> { bool a; };

int main() {
    foo<100000000000000 * 10240> a;
}
                                                                                                                                                                                                                                                                                                                            jankorous @ jans-imac /Users/jankorous/tmp
> ~/src/oss/llvm@master/build/bin/clang++ tmpl_overflow.hpp
tmpl_overflow.hpp:3:23: error: non-type template argument evaluates to 1024000000000000000, which cannot be narrowed to type 'int' [-Wc++11-narrowing]
template<> struct foo<100000000000000 * 10240> { bool a; };
                      ^
tmpl_overflow.hpp:6:9: error: non-type template argument evaluates to 1024000000000000000, which cannot be narrowed to type 'int' [-Wc++11-narrowing]
    foo<100000000000000 * 10240> a;
        ^
2 errors generated.

Ping for reviewers.

This revision is now accepted and ready to land.Mar 26 2018, 3:07 PM
This revision was automatically updated to reflect the committed changes.