Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-21297

Clang code model: False unused-function warning

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • P3: Somewhat important
    • None
    • Qt Creator 4.7.1, Qt Creator 4.8.0-beta2
    • C/C++/Obj-C++ Support
    • None

    Description

      The clang code model highlights a function in the editor with an unused-function warning although the function is used when compiling the code.

      I think the problem is somehow related to the way qt creator parses multiple files, because it can only be reproduced, if certain conditions are met. See my sample code:

      #include <stdio.h>
      
      /// In a header file
      struct S {};
      
      template <typename T>
      const S &operator<<(const S &s, const T &l)
      {
      	return s << l;
      }
      /// In a cpp file
      
      int main()
      {
      	S() << 0;
      }
      
      static const S &operator<<(const S &s, int x) {
      	(void) x;
      	printf("operator<<(const S &, int)\n");
      	return s;
      }
      

      The warning is emitted for const S &operator<<(const S &s, int x).
      If either the declaration of struct S or of the template is moved to the cpp file, no warning is emitted. In both cases the code compiles just fine with all compilers i've tested.

      I am not really sure if the code is even valid c++, because the operator<<(S,int) should not be visible to the template operator. If I change it to a normal function, it does in fact not compile (at least with gcc  4.8.7, but msvc2017 compiles). Adding a declaration for the non template operator between the declaration of the struct and the template operator removes the warning.

      Still I think it is a bug and it may have other consequences, because the code compiles just fine, even with the clang version shipped together with the creator

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kandeler Christian Kandeler
            jvehlow jvehlow
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes