From cb2cce5139ac3429088ecef011198c0e218618cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com> Date: Thu, 5 Mar 2009 17:12:31 +0100 Subject: [PATCH] Adapt generated header guards to the filename Use _HPP for example for .hpp files, for example. Based on simply putting the chosen extension in uppercase. --- src/libs/utils/codegeneration.cpp | 20 ++++++++++++++-- src/plugins/cppeditor/cppclasswizard.cpp | 2 +- src/plugins/cppeditor/cppfilewizard.cpp | 23 ++++--------------- src/plugins/cppeditor/cppfilewizard.h | 1 - .../cpp/formclasswizardparameters.cpp | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/libs/utils/codegeneration.cpp b/src/libs/utils/codegeneration.cpp index 411ee33280e..14448ea4b75 100644 --- a/src/libs/utils/codegeneration.cpp +++ b/src/libs/utils/codegeneration.cpp @@ -36,10 +36,26 @@ namespace Core { namespace Utils { +static QString toAlphaNum(const QString &s) +{ + QString rc; + const int len = s.size(); + const QChar underscore = QLatin1Char('_'); + + for (int i = 0; i < len; i++) { + const QChar c = s.at(i); + if (c == underscore || c.isLetterOrNumber()) + rc += c; + } + return rc; +} + QWORKBENCH_UTILS_EXPORT QString headerGuard(const QString &file) { - QString rc = QFileInfo(file).baseName().toUpper(); - rc += QLatin1String("_H"); + const QFileInfo fi(file); + QString rc = toAlphaNum(fi.completeBaseName()).toUpper(); + rc += QLatin1Char('_'); + rc += toAlphaNum(fi.suffix()).toUpper(); return rc; } diff --git a/src/plugins/cppeditor/cppclasswizard.cpp b/src/plugins/cppeditor/cppclasswizard.cpp index d57b9555dcb..a5f21f859bf 100644 --- a/src/plugins/cppeditor/cppclasswizard.cpp +++ b/src/plugins/cppeditor/cppclasswizard.cpp @@ -183,7 +183,7 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par return false; const QString unqualifiedClassName = namespaceList.takeLast(); - const QString guard = Core::Utils::headerGuard(unqualifiedClassName); + const QString guard = Core::Utils::headerGuard(params.headerFile); // == Header file == QTextStream headerStr(header); diff --git a/src/plugins/cppeditor/cppfilewizard.cpp b/src/plugins/cppeditor/cppfilewizard.cpp index a7242739e34..2683e2f6c23 100644 --- a/src/plugins/cppeditor/cppfilewizard.cpp +++ b/src/plugins/cppeditor/cppfilewizard.cpp @@ -31,6 +31,8 @@ #include "cppeditor.h" #include "cppeditorconstants.h" +#include <utils/codegeneration.h> + #include <QtCore/QTextStream> #include <QtCore/QFileInfo> #include <QtCore/QDebug> @@ -48,20 +50,6 @@ CppFileWizard::CppFileWizard(const BaseFileWizardParameters ¶meters, { } -QString CppFileWizard::toAlphaNum(const QString &s) -{ - QString rc; - const int len = s.size(); - const QChar underscore = QLatin1Char('_'); - - for (int i = 0; i < len; i++) { - const QChar c = s.at(i); - if (c == underscore || c.isLetterOrNumber()) - rc += c; - } - return rc; -} - Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path, const QString &name, QString * /*errorMessage*/) const @@ -69,11 +57,11 @@ Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path, { const QString mimeType = m_type == Source ? QLatin1String(Constants::CPP_SOURCE_MIMETYPE) : QLatin1String(Constants::CPP_HEADER_MIMETYPE); const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(mimeType)); + Core::GeneratedFile file(fileName); file.setEditorKind(QLatin1String(Constants::C_CPPEDITOR)); - const QString cleanName = toAlphaNum(QFileInfo(name).baseName()); - file.setContents(fileContents(m_type, fileName)); + return Core::GeneratedFiles() << file; } @@ -84,8 +72,7 @@ QString CppFileWizard::fileContents(FileType type, const QString &fileName) cons QTextStream str(&contents); switch (type) { case Header: { - QString guard = toAlphaNum(baseName).toUpper(); - guard += QLatin1String("_H"); + const QString guard = Core::Utils::headerGuard(fileName); str << QLatin1String("#ifndef ") << guard << QLatin1String("\n#define ") << guard << QLatin1String("\n\n#endif // ") << guard << QLatin1String("\n"); diff --git a/src/plugins/cppeditor/cppfilewizard.h b/src/plugins/cppeditor/cppfilewizard.h index 7ddf603919f..c338973a8d1 100644 --- a/src/plugins/cppeditor/cppfilewizard.h +++ b/src/plugins/cppeditor/cppfilewizard.h @@ -49,7 +49,6 @@ public: QObject *parent = 0); protected: - static QString toAlphaNum(const QString &s); QString fileContents(FileType type, const QString &baseName) const; protected: diff --git a/src/plugins/designer/cpp/formclasswizardparameters.cpp b/src/plugins/designer/cpp/formclasswizardparameters.cpp index a67d4727fb9..2c2cde09cf3 100644 --- a/src/plugins/designer/cpp/formclasswizardparameters.cpp +++ b/src/plugins/designer/cpp/formclasswizardparameters.cpp @@ -73,7 +73,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in const QString unqualifiedClassName = namespaceList.takeLast(); // Include guards - const QString guard = Core::Utils::headerGuard(unqualifiedClassName); + const QString guard = Core::Utils::headerGuard(headerFile); QString uiInclude = QLatin1String("ui_"); uiInclude += QFileInfo(uiFile).baseName(); -- GitLab