【发布时间】: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