Commit dcccb4cb authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: Use CompilerOptionsBuilder as an object

Makes the client code more readable.

Change-Id: Ie98ba93c758643039d3ebdc7550b1c4ac9473298
Reviewed-by: default avatarMarco Bubke <marco.bubke@theqtcompany.com>
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@theqtcompany.com>
parent d41c634b
......@@ -139,49 +139,53 @@ static bool maybeIncludeBorlandExtensions()
*/
QStringList createClangOptions(const ProjectPart::Ptr &pPart, ProjectFile::Kind fileKind)
{
QStringList result;
if (pPart.isNull())
return result;
return QStringList();
CompilerOptionsBuilder optionsBuilder(pPart);
if (verboseRunLog().isDebugEnabled())
result << QLatin1String("-v");
optionsBuilder.add(QLatin1String("-v"));
const bool objcExt = pPart->languageExtensions & ProjectPart::ObjectiveCExtensions;
result << CompilerOptionsBuilder::createLanguageOption(fileKind, objcExt);
result << CompilerOptionsBuilder::createOptionsForLanguage(
pPart->languageVersion,
pPart->languageExtensions,
maybeIncludeBorlandExtensions());
result << CompilerOptionsBuilder::createDefineOptions(pPart->toolchainDefines);
result << CompilerOptionsBuilder::createDefineOptions(pPart->projectDefines);
optionsBuilder.addLanguageOption(fileKind);
optionsBuilder.addOptionsForLanguage(maybeIncludeBorlandExtensions());
optionsBuilder.addToolchainAndProjectDefines();
static const QString resourceDir = getResourceDir();
if (!resourceDir.isEmpty()) {
result << QLatin1String("-nostdlibinc");
result << (QLatin1String("-I") + resourceDir);
result << QLatin1String("-undef");
optionsBuilder.add(QLatin1String("-nostdlibinc"));
optionsBuilder.add(QLatin1String("-I") + resourceDir);
optionsBuilder.add(QLatin1String("-undef"));
}
result << CompilerOptionsBuilder::createHeaderPathOptions(pPart->headerPaths, isBlacklisted);
optionsBuilder.addHeaderPathOptions(isBlacklisted);
// Inject header file
static const QString injectedHeader = ICore::instance()->resourcePath()
+ QLatin1String("/cplusplus/qt%1-qobjectdefs-injected.h");
// if (qtVersion == ProjectPart::Qt4)
// opts << QLatin1String("-include") << injectedHeader.arg(QLatin1Char('4'));
if (pPart->qtVersion == ProjectPart::Qt5)
result << QLatin1String("-include") << injectedHeader.arg(QLatin1Char('5'));
if (!pPart->projectConfigFile.isEmpty())
result << QLatin1String("-include") << pPart->projectConfigFile;
result << QLatin1String("-fmessage-length=0");
result << QLatin1String("-fdiagnostics-show-note-include-stack");
result << QLatin1String("-fmacro-backtrace-limit=0");
result << QLatin1String("-fretain-comments-from-system-headers");
// if (pPart->qtVersion == ProjectPart::Qt4) {
// builder.addOption(QLatin1String("-include"));
// builder.addOption(injectedHeader.arg(QLatin1Char('4')));
// }
if (pPart->qtVersion == ProjectPart::Qt5) {
optionsBuilder.add(QLatin1String("-include"));
optionsBuilder.add(injectedHeader.arg(QLatin1Char('5')));
}
if (!pPart->projectConfigFile.isEmpty()) {
optionsBuilder.add(QLatin1String("-include"));
optionsBuilder.add(pPart->projectConfigFile);
}
optionsBuilder.add(QLatin1String("-fmessage-length=0"));
optionsBuilder.add(QLatin1String("-fdiagnostics-show-note-include-stack"));
optionsBuilder.add(QLatin1String("-fmacro-backtrace-limit=0"));
optionsBuilder.add(QLatin1String("-fretain-comments-from-system-headers"));
// TODO: -Xclang -ferror-limit -Xclang 0 ?
return result;
return optionsBuilder.options();
}
/// @return Option to speed up parsing with precompiled header
......
......@@ -501,9 +501,23 @@ void ProjectPartBuilder::createProjectPart(const QStringList &theSources,
}
QStringList CompilerOptionsBuilder::createHeaderPathOptions(
const ProjectPart::HeaderPaths &headerPaths,
IsBlackListed isBlackListed, const QString &toolchainType)
CompilerOptionsBuilder::CompilerOptionsBuilder(const ProjectPart::Ptr &projectPart)
: m_projectPart(projectPart)
{
}
QStringList CompilerOptionsBuilder::options() const
{
return m_options;
}
void CompilerOptionsBuilder::add(const QString &option)
{
m_options.append(option);
}
void CompilerOptionsBuilder::addHeaderPathOptions(IsBlackListed isBlackListed,
const QString &toolchainType)
{
typedef ProjectPart::HeaderPath HeaderPath;
const QString defaultPrefix
......@@ -511,7 +525,7 @@ QStringList CompilerOptionsBuilder::createHeaderPathOptions(
QStringList result;
foreach (const HeaderPath &headerPath , headerPaths) {
foreach (const HeaderPath &headerPath , m_projectPart->headerPaths) {
if (headerPath.path.isEmpty())
continue;
......@@ -533,14 +547,12 @@ QStringList CompilerOptionsBuilder::createHeaderPathOptions(
result.append(prefix + headerPath.path);
}
return result;
m_options.append(result);
}
QStringList CompilerOptionsBuilder::createDefineOptions(const QByteArray &defines,
bool toolchainDefines,
const QString &toolchainType)
void CompilerOptionsBuilder::addToolchainAndProjectDefines(const QString &toolchainType)
{
QByteArray extendedDefines = defines;
QByteArray extendedDefines = m_projectPart->toolchainDefines + m_projectPart->projectDefines;
QStringList result;
// In gcc headers, lots of built-ins are referenced that clang does not understand.
......@@ -559,17 +571,6 @@ QStringList CompilerOptionsBuilder::createDefineOptions(const QByteArray &define
if (def.startsWith("#define __cplusplus"))
continue;
// TODO: verify if we can pass compiler-defined macros when also passing -undef.
if (toolchainDefines) {
//### FIXME: the next 3 check shouldn't be needed: we probably don't want to get the compiler-defined defines in.
if (!def.startsWith("#define "))
continue;
if (def.startsWith("#define _"))
continue;
if (def.startsWith("#define OBJC_NEW_PROPERTIES"))
continue;
}
// gcc 4.9 has:
// #define __has_include(STR) __has_include__(STR)
// #define __has_include_next(STR) __has_include_next__(STR)
......@@ -590,7 +591,7 @@ QStringList CompilerOptionsBuilder::createDefineOptions(const QByteArray &define
result.append(arg);
}
return result;
m_options.append(result);
}
static QStringList createLanguageOptionGcc(ProjectFile::Kind fileKind, bool objcExt)
......@@ -668,24 +669,26 @@ static QStringList createLanguageOptionMsvc(ProjectFile::Kind fileKind)
return opts;
}
QStringList CompilerOptionsBuilder::createLanguageOption(ProjectFile::Kind fileKind, bool objcExt,
const QString &toolchainType)
void CompilerOptionsBuilder::addLanguageOption(ProjectFile::Kind fileKind,
const QString &toolchainType)
{
return toolchainType == QLatin1String("msvc") ? createLanguageOptionMsvc(fileKind)
: createLanguageOptionGcc(fileKind, objcExt);
const bool objcExt = m_projectPart->languageExtensions & ProjectPart::ObjectiveCExtensions;
const QStringList options = toolchainType == QLatin1String("msvc")
? createLanguageOptionMsvc(fileKind)
: createLanguageOptionGcc(fileKind, objcExt);
m_options.append(options);
}
QStringList CompilerOptionsBuilder::createOptionsForLanguage(
ProjectPart::LanguageVersion languageVersion,
ProjectPart::LanguageExtensions languageExtensions,
bool checkForBorlandExtensions,
const QString &toolchainType)
void CompilerOptionsBuilder::addOptionsForLanguage(bool checkForBorlandExtensions,
const QString &toolchainType)
{
QStringList opts;
if (toolchainType == QLatin1String("msvc"))
return opts;
bool gnuExtensions = languageExtensions & ProjectPart::GnuExtensions;
switch (languageVersion) {
return;
const ProjectPart::LanguageExtensions languageExtensions = m_projectPart->languageExtensions;
const bool gnuExtensions = languageExtensions & ProjectPart::GnuExtensions;
switch (m_projectPart->languageVersion) {
case ProjectPart::C89:
opts << (gnuExtensions ? QLatin1String("-std=gnu89") : QLatin1String("-std=c89"));
break;
......@@ -718,5 +721,5 @@ QStringList CompilerOptionsBuilder::createOptionsForLanguage(
if (checkForBorlandExtensions && (languageExtensions & ProjectPart::BorlandExtensions))
opts << QLatin1String("-fborland-extensions");
return opts;
m_options.append(opts);
}
......@@ -214,21 +214,24 @@ private:
class CPPTOOLS_EXPORT CompilerOptionsBuilder
{
public:
CompilerOptionsBuilder(const ProjectPart::Ptr &projectPart);
QStringList options() const;
void add(const QString &option);
typedef std::function<bool (const QString &)> IsBlackListed;
static QStringList createHeaderPathOptions(const ProjectPart::HeaderPaths &headerPaths,
IsBlackListed isBlackListed = IsBlackListed(),
const QString &toolchainType = QLatin1String("clang"));
static QStringList createDefineOptions(const QByteArray &defines,
bool toolchainDefines = false,
const QString &toolchainType = QLatin1String("clang"));
static QStringList createLanguageOption(ProjectFile::Kind fileKind, bool objcExt,
const QString &toolchainType = QLatin1String("clang"));
static QStringList createOptionsForLanguage(ProjectPart::LanguageVersion languageVersion,
ProjectPart::LanguageExtensions languageExtensions,
bool checkForBorlandExtensions = true,
const QString &toolchainType = QLatin1String("clang"));
void addHeaderPathOptions(IsBlackListed isBlackListed = IsBlackListed(),
const QString &toolchainType = QLatin1String("clang"));
void addToolchainAndProjectDefines(const QString &toolchainType = QLatin1String("clang"));
void addLanguageOption(ProjectFile::Kind fileKind,
const QString &toolchainType = QLatin1String("clang"));
void addOptionsForLanguage(bool checkForBorlandExtensions = true,
const QString &toolchainType = QLatin1String("clang"));
private:
ProjectPart::Ptr m_projectPart;
QStringList m_options;
};
} // namespace CppTools
......
Markdown is supported
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