【问题标题】:Bringing Cppcheck and VCL together将 Cppcheck 和 VCL 结合在一起
【发布时间】:2021-01-22 05:52:17
【问题描述】:

Cppcheck 现在有一项检查,可以检测对临时引用的引用,这会导致涉及 VCL 类(C++Builder6)的代码误报 (danglingTemporaryLifetime)。

这里是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&);

};

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

一个在 BCB6 中编译的示例,如果像这样调用

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

重现错误:

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";
        ^

learned that one should not provide the include path to the standard library 也从未尝试为 VCL 这样做。但现在 Cppcheck 无法处理未知类的 Caption 属性。为下一个成员访问(已知的Button1)给出错误,并且分配给它的临时成员的未知成员在这里仍然有效。

我还尝试通过添加来告诉 VCL 包含 Cppcheck 的路径

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

但这会导致

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)'
^

如何解决这个问题?

【问题讨论】:

  • __property 是 Borland/Embarcadero C++ 编译器中的非标准扩展。有很多与__property 相关的语义,因此除非您可以更改 CppCheck 的源代码或创建自定义插件来处理这些语义,否则您不太可能能够使其正确处理属性。

标签: properties vcl cppcheck temporary-objects false-positive


【解决方案1】:

Cppcheck 对此类属性进行了一些处理。我不想让 Cppcheck 为你的代码写垃圾。

你能告诉我如何复制吗?我已将您的代码放入文件 1.cpp 并使用命令 cppcheck --enable=style --inconclusive 1.cpp 我没有收到任何警告。

【讨论】:

  • 感谢您迄今为止的帮助。我现在设法得到一个工作示例。我了解了一些有关实际原因的信息,但还没有解决问题。我也完全修改了这个问题。 (很抱歉造成这么多麻烦。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多