Commit 35ea746d authored by Milian Wolff's avatar Milian Wolff
Browse files

Workaround bogus libclang error of supposed unusual deallocations

Add the `-fsized-deallocation` argument to libclang to allow
better compat with GCC 12 / libstdc++ code that uses sized
deallocation in some core areas. Fixes bogus warnings like:

```
call to '__builtin_operator_delete' selects non-usual deallocation function
```

See also:
https://youtrack.jetbrains.com/issue/CPP-29091/In-template-call-to-builtinoperatordelete-selects-non-usual-deallocation-function-gcc-12#focus=Comments-27-6067190.0-0
parent ce41c99b
......@@ -61,6 +61,9 @@ void sanitizeArguments(QVector<QByteArray>& arguments)
// Note how clang warns us about emulating GCC, which is exactly what we want here.
const auto noGnuZeroVaridicMacroArguments = QByteArrayLiteral("-Wno-gnu-zero-variadic-macro-arguments");
bool noGnuZeroVaridicMacroArgumentsFound = false;
bool isCpp = false;
const auto sizedDealloc = QByteArrayLiteral("-fsized-deallocation");
bool sizedDeallocFound = false;
for (auto& argument : arguments) {
if (argument == "-Werror") {
argument.clear();
......@@ -75,12 +78,21 @@ void sanitizeArguments(QVector<QByteArray>& arguments)
#endif
else if (!noGnuZeroVaridicMacroArgumentsFound && argument == noGnuZeroVaridicMacroArguments) {
noGnuZeroVaridicMacroArgumentsFound = true;
} else if (!isCpp && argument == "-xc++") {
isCpp = true;
} else if (!sizedDeallocFound && argument == sizedDealloc) {
sizedDeallocFound = true;
}
}
if (!noGnuZeroVaridicMacroArgumentsFound) {
arguments.append(noGnuZeroVaridicMacroArguments);
}
if (isCpp && !sizedDeallocFound) {
// see e.g.
// https://youtrack.jetbrains.com/issue/CPP-29091/In-template-call-to-builtinoperatordelete-selects-non-usual-deallocation-function-gcc-12#focus=Comments-27-6067190.0-0
arguments.append(sizedDealloc);
}
}
QVector<QByteArray> argsForSession(const QString& path, ParseSessionData::Options options, const ParserSettings& parserSettings)
......
......@@ -2096,24 +2096,46 @@ int main()
QCOMPARE(top->localDeclarations().count(), 3);
}
void TestDUChain::testGccCompatibility_data()
{
QTest::addColumn<QString>("parserArguments");
QTest::addColumn<QString>("code");
QTest::newRow("x86intrin") << QString() << QStringLiteral(R"(
#include <x86intrin.h>
int main() { return 0; }
)");
QTest::newRow("sized-dealloc") << QStringLiteral("-Wall -std=c++14") << QStringLiteral(R"(
#include <memory>
int main() {
auto test = std::make_shared<uint32_t>();
return 0;
}
)");
}
void TestDUChain::testGccCompatibility()
{
QFETCH(QString, parserArguments);
QFETCH(QString, code);
// TODO: make it easier to change the compiler provider for testing purposes
QTemporaryDir dir;
auto project = new TestProject(Path(dir.path()), this);
auto definesAndIncludesConfig = project->projectConfiguration()->group("CustomDefinesAndIncludes");
auto pathConfig = definesAndIncludesConfig.group("ProjectPath0");
pathConfig.writeEntry("Path", ".");
if (!parserArguments.isEmpty()) {
pathConfig.writeEntry("parserArguments", parserArguments);
}
pathConfig.group("Compiler").writeEntry("Name", "GCC");
m_projectController->addProject(project);
{
// TODO: Also test in C mode. Currently it doesn't work (some intrinsics missing?)
TestFile file(QStringLiteral(R"(
#include <x86intrin.h>
int main() { return 0; }
)"), QStringLiteral("cpp"), project, dir.path());
TestFile file(code, QStringLiteral("cpp"), project, dir.path());
file.parse();
QVERIFY(file.waitForParsed(50000));
......
......@@ -91,6 +91,7 @@ private Q_SLOTS:
void testProblemRequestedHereSameFile();
void testProblemRequestedHereChain();
void testGccCompatibility_data();
void testGccCompatibility();
void testQtIntegration();
void testHasInclude();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment