0

Cppcheck now has a check to detect references to temporary which leads to false positives (danglingTemporaryLifetime) for code involving VCL classes (of C++Builder6).

Here is test.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#include <string>
//---------------------------------------------------------------------------

class TForm1 : public TForm
{
__published:
    TButton *Button1;
public:
    explicit __fastcall TForm1(TComponent* Owner);
};

//---------------------------------------------------------------------------

class TTest
{
public:
    std::string GetText() const;
    void Init()
    {
        m_form->Caption = GetText().c_str();
        m_form->Button1->Caption = "Text";
    }
    TForm1* m_form;

    // Forbidden:
    TTest(const TTest&);
    TTest& operator=(const TTest&);

};

//---------------------------------------------------------------------------

an example that compiles in BCB6 and, if invoked like this

"C:\Program Files\Cppcheck\cppcheck.exe" --enable=style --inconclusive test.cpp

reproduces the error:

Checking test.cpp ...
test.cpp:23:9: error: Using pointer to temporary. [danglingTemporaryLifetime]
        m_form->Button1->Caption = "Text";
        ^
test.cpp:22:34: note: Pointer to container is created here.
        m_form->Caption = GetText().c_str();
                                 ^
test.cpp:23:9: note: Using pointer to temporary.
        m_form->Button1->Caption = "Text";
        ^

I learned that one should not provide the include path to the standard library and never tried to do it for the VCL either. But now Cppcheck doesn't cope with the Caption property of a class that's not known. The error is given for the next member access (that to Button1 which is known) and the unknown member with the temporary assigned to it still has an effect here.

I also tried to tell the VCL include path to Cppcheck by adding

 -I "C:\Program Files (x86)\Borland\CBuilder6\Include\Vcl"

But this leads to

C:\Program Files (x86)\Borland\CBuilder6\Include\Vcl\sysvari.h:3365:0: error: No pair for character ("). Can't process file. File is either invalid or unicode, which
is currently not supported. [preprocessorErrorDirective]
#pragma message "ERROR: sizeof(TVarData) < sizeof(VARIANT)'
^

How to solve this problem?

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • `__property` is a non-standard extension in the Borland/Embarcadero C++ compilers. There are quite a few semantics related to `__property`, so unless you can alter CppCheck's source code, or create a custom addon, to handle those semantics, it is not likely you are going to be able to make it handle properties correctly. – Remy Lebeau Oct 07 '20 at 19:03

1 Answers1

1

Cppcheck has a bit of handling of such class properties. I don't want Cppcheck to write garbage for your code.

Can you show me how to reproduce? I have put your code in a file 1.cpp and with the command cppcheck --enable=style --inconclusive 1.cpp I get no warning.

Daniel Marjamäki
  • 2,907
  • 15
  • 16