forked from qt-creator/qt-creator
CppEditor: Ensure "inline" specifier
... when creating functions in header files. Otherwise, we will likely cause linker failures in non-trivial projects. Fixes: QTCREATORBUG-15052 Change-Id: Ic0fff8779ba924f8b9943ab233a0cda409e73e9d Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -108,6 +108,7 @@ private slots:
|
||||
void test_quickfix_GenerateGetterSetter_onlyGetter();
|
||||
void test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet();
|
||||
void test_quickfix_GenerateGetterSetter_onlySetter();
|
||||
void test_quickfix_GenerateGetterSetter_onlySetterHeaderFile();
|
||||
void test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent();
|
||||
void test_quickfix_GenerateGetterSetter_offerSetterWhenGetterPresent();
|
||||
void test_quickfix_GenerateGettersSetters_data();
|
||||
|
||||
@@ -2242,6 +2242,31 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetter()
|
||||
QuickFixOperationTest(testDocuments, &factory, ProjectExplorer::HeaderPaths(), 2);
|
||||
}
|
||||
|
||||
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetterHeaderFile()
|
||||
{
|
||||
QList<QuickFixTestDocument::Ptr> testDocuments;
|
||||
const QByteArray original =
|
||||
"class Foo\n"
|
||||
"{\n"
|
||||
"public:\n"
|
||||
" int bar@;\n"
|
||||
"};\n";
|
||||
const QByteArray expected =
|
||||
"class Foo\n"
|
||||
"{\n"
|
||||
"public:\n"
|
||||
" int bar@;\n"
|
||||
" void setBar(int value);\n"
|
||||
"};\n\n"
|
||||
"inline void Foo::setBar(int value)\n"
|
||||
"{\n"
|
||||
" bar = value;\n"
|
||||
"}\n";
|
||||
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
|
||||
GenerateGetterSetter factory;
|
||||
QuickFixOperationTest(testDocuments, &factory, ProjectExplorer::HeaderPaths(), 2);
|
||||
}
|
||||
|
||||
class CppCodeStyleSettingsChanger {
|
||||
public:
|
||||
CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings);
|
||||
@@ -2457,17 +2482,17 @@ private:
|
||||
int bar2_;
|
||||
QString bar3;
|
||||
};
|
||||
void Foo::setBar2(int bar2)
|
||||
inline void Foo::setBar2(int bar2)
|
||||
{
|
||||
bar2_ = bar2;
|
||||
}
|
||||
|
||||
QString Foo::getBar3() const
|
||||
inline QString Foo::getBar3() const
|
||||
{
|
||||
return bar3;
|
||||
}
|
||||
|
||||
void Foo::setBar3(const QString &value)
|
||||
inline void Foo::setBar3(const QString &value)
|
||||
{
|
||||
bar3 = value;
|
||||
}
|
||||
@@ -2513,7 +2538,7 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_afterClass()
|
||||
" void a();\n"
|
||||
"};\n"
|
||||
"\n"
|
||||
"void Foo::a()\n"
|
||||
"inline void Foo::a()\n"
|
||||
"{\n\n}\n"
|
||||
"\n"
|
||||
"class Bar {};\n";
|
||||
@@ -4422,7 +4447,7 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutside2()
|
||||
" void f3();\n"
|
||||
"};\n"
|
||||
"\n"
|
||||
"int Foo::f2()\n"
|
||||
"inline int Foo::f2()\n"
|
||||
"{\n"
|
||||
" return 1;\n"
|
||||
"}\n";
|
||||
@@ -4728,7 +4753,7 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_afterClass()
|
||||
" void a();\n"
|
||||
"};\n"
|
||||
"\n"
|
||||
"void Foo::a() {}\n"
|
||||
"inline void Foo::a() {}\n"
|
||||
"\n"
|
||||
"class Bar {};\n";
|
||||
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
|
||||
@@ -5017,7 +5042,7 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCpp()
|
||||
// Header File
|
||||
original = "int number() const;\n";
|
||||
expected =
|
||||
"int number() const\n"
|
||||
"inline int number() const\n"
|
||||
"{\n"
|
||||
" return 5;\n"
|
||||
"}\n";
|
||||
@@ -5053,7 +5078,7 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS()
|
||||
"}\n";
|
||||
expected =
|
||||
"namespace MyNamespace {\n"
|
||||
"int number() const\n"
|
||||
"inline int number() const\n"
|
||||
"{\n"
|
||||
" return 5;\n"
|
||||
"}\n"
|
||||
@@ -5442,7 +5467,7 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data()
|
||||
"{\n"
|
||||
" @{start}g();@{end}\n"
|
||||
"}\n")
|
||||
<< _("void extracted()\n"
|
||||
<< _("inline void extracted()\n"
|
||||
"{\n"
|
||||
" g();\n"
|
||||
"}\n"
|
||||
@@ -5469,7 +5494,7 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data()
|
||||
"private:\n"
|
||||
" void bar();\n"
|
||||
"};\n\n"
|
||||
"void Foo::extracted()\n"
|
||||
"inline void Foo::extracted()\n"
|
||||
"{\n"
|
||||
" g();\n"
|
||||
"}\n\n"
|
||||
@@ -5496,7 +5521,7 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data()
|
||||
" void extracted(NS::C &c);\n" // TODO: Remove non-required qualification
|
||||
"};\n"
|
||||
"}\n"
|
||||
"void NS::C::extracted(NS::C &c)\n"
|
||||
"inline void NS::C::extracted(NS::C &c)\n"
|
||||
"{\n"
|
||||
" C *c = &c;\n"
|
||||
"}\n"
|
||||
|
||||
@@ -114,6 +114,15 @@ const QList<CppQuickFixFactory *> &CppQuickFixFactory::cppQuickFixFactories()
|
||||
|
||||
namespace Internal {
|
||||
|
||||
QString inlinePrefix(const QString &targetFile, const std::function<bool()> &extraCondition = {})
|
||||
{
|
||||
if (ProjectFile::isHeader(ProjectFile::classify(targetFile))
|
||||
&& (!extraCondition || extraCondition())) {
|
||||
return "inline ";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// In the following anonymous namespace all functions are collected, which could be of interest for
|
||||
// different quick fixes.
|
||||
namespace {
|
||||
@@ -2831,8 +2840,10 @@ public:
|
||||
}
|
||||
const QString name = oo.prettyName(LookupContext::minimalName(m_decl, targetCoN,
|
||||
control));
|
||||
|
||||
const QString defText = oo.prettyType(tn, name) + QLatin1String("\n{\n\n}");
|
||||
const QString defText = inlinePrefix(
|
||||
m_targetFileName, [this] { return m_defpos == DefPosOutsideClass; })
|
||||
+ oo.prettyType(tn, name)
|
||||
+ QLatin1String("\n{\n\n}");
|
||||
|
||||
const int targetPos = targetFile->position(m_loc.line(), m_loc.column());
|
||||
const int targetPos2 = qMax(0, targetFile->position(m_loc.line(), 1) - 1);
|
||||
@@ -3290,14 +3301,15 @@ public:
|
||||
// Construct implementation strings
|
||||
const QString implementationGetterTypeAndNameString = oo.prettyType(
|
||||
getterType, QString::fromLatin1("%1::%2").arg(classString, getterName));
|
||||
const QString implementationGetter = QString::fromLatin1("%1()%2\n"
|
||||
const QString inlineSpecifier = sameFile && wasHeader ? QString("inline") : QString();
|
||||
const QString implementationGetter = QString::fromLatin1("%4 %1()%2\n"
|
||||
"{\n"
|
||||
"return %3;\n"
|
||||
"}")
|
||||
.arg(implementationGetterTypeAndNameString,
|
||||
isStatic ? QString() : QLatin1String(" const"),
|
||||
rawName);
|
||||
const QString implementationSetter = QString::fromLatin1("void %1::%2(%3)\n"
|
||||
rawName, inlineSpecifier);
|
||||
const QString implementationSetter = QString::fromLatin1("%6 void %1::%2(%3)\n"
|
||||
"{\n"
|
||||
"%4 = %5;\n"
|
||||
"}")
|
||||
@@ -3305,7 +3317,8 @@ public:
|
||||
setterName,
|
||||
paramString,
|
||||
rawName,
|
||||
paramName);
|
||||
paramName,
|
||||
inlineSpecifier);
|
||||
|
||||
QString implementation;
|
||||
if (generateGetter())
|
||||
@@ -3872,6 +3885,7 @@ public:
|
||||
}
|
||||
funcDef.append(QLatin1String("\n}\n\n"));
|
||||
funcDef.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
|
||||
funcDef.prepend(inlinePrefix(currentFile->fileName()));
|
||||
funcCall.append(QLatin1Char(';'));
|
||||
|
||||
// Get starting indentation from original code.
|
||||
@@ -5288,7 +5302,9 @@ public:
|
||||
Scope *scopeAtInsertPos = m_toFile->cppDocument()->scopeAt(l.line(), l.column());
|
||||
|
||||
// construct definition
|
||||
const QString funcDec = definitionSignature(m_operation, funcAST, m_fromFile, m_toFile,
|
||||
const QString funcDec = inlinePrefix(
|
||||
m_toFile->fileName(), [this] { return m_type == MoveOutside; })
|
||||
+ definitionSignature(m_operation, funcAST, m_fromFile, m_toFile,
|
||||
scopeAtInsertPos);
|
||||
QString funcDef = prefix + funcDec;
|
||||
const int startPosition = m_fromFile->endOf(funcAST->declarator);
|
||||
@@ -5686,10 +5702,12 @@ void MoveFuncDefToDecl::match(const CppQuickFixInterface &interface, QuickFixOpe
|
||||
}
|
||||
}
|
||||
|
||||
if (!declText.isEmpty())
|
||||
if (!declText.isEmpty()) {
|
||||
declText.prepend(inlinePrefix(declFileName));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!declFileName.isEmpty() && !declText.isEmpty())
|
||||
result << new MoveFuncDefToDeclOp(interface,
|
||||
|
||||
Reference in New Issue
Block a user