cppprojects.cpp 23 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30 31 32 33
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "cppprojects.h"

#include <projectexplorer/headerpath.h>
34 35 36
#include <projectexplorer/kit.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
37
#include <projectexplorer/project.h>
38 39
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
40

41
#include <utils/fileutils.h>
42 43
#include <utils/qtcassert.h>

44
#include <QDir>
45 46 47
#include <QSet>
#include <QTextStream>

48
using namespace CppTools;
49
using namespace CppTools::Internal;
50
using namespace ProjectExplorer;
51 52 53

ProjectPart::ProjectPart()
    : project(0)
54 55
    , languageVersion(CXX14)
    , languageExtensions(NoExtensions)
56
    , qtVersion(UnknownQt)
57
    , warningFlags(ToolChain::WarningsDefault)
58
    , selectedForBuilding(true)
59 60 61 62 63 64 65 66 67 68
{
}

/*!
    \brief Retrieves info from concrete compiler using it's flags.

    \param tc Either nullptr or toolchain for project's active target.
    \param cxxflags C++ or Objective-C++ flags.
    \param cflags C or ObjectiveC flags if possible, \a cxxflags otherwise.
*/
69
void ProjectPart::evaluateToolchain(const ToolChain *tc,
70
                                    const QStringList &commandLineFlags,
71 72 73 74 75 76
                                    const Utils::FileName &sysRoot)
{
    if (!tc)
        return;

    using namespace ProjectExplorer;
77 78 79 80 81 82 83 84 85 86 87 88
    ToolChain::CompilerFlags flags = tc->compilerFlags(commandLineFlags);

    if (flags & ToolChain::StandardC11)
        languageVersion = C11;
    else if (flags & ToolChain::StandardC99)
        languageVersion = C99;
    else if (flags & ToolChain::StandardCxx17)
        languageVersion = CXX17;
    else if (flags & ToolChain::StandardCxx14)
        languageVersion = CXX14;
    else if (flags & ToolChain::StandardCxx11)
        languageVersion = CXX11;
89 90
    else if (flags & ToolChain::StandardCxx98)
        languageVersion = CXX98;
91
    else
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        languageVersion = CXX11;

    if (flags & ToolChain::BorlandExtensions)
        languageExtensions |= BorlandExtensions;
    if (flags & ToolChain::GnuExtensions)
        languageExtensions |= GnuExtensions;
    if (flags & ToolChain::MicrosoftExtensions)
        languageExtensions |= MicrosoftExtensions;
    if (flags & ToolChain::OpenMP)
        languageExtensions |= OpenMPExtensions;
    if (flags & ToolChain::ObjectiveC)
        languageExtensions |= ObjectiveCExtensions;

    warningFlags = tc->warningFlags(commandLineFlags);

    const QList<ProjectExplorer::HeaderPath> headers = tc->systemHeaderPaths(commandLineFlags, sysRoot);
108 109 110 111 112 113 114
    foreach (const ProjectExplorer::HeaderPath &header, headers) {
        headerPaths << ProjectPart::HeaderPath(header.path(),
                                header.kind() == ProjectExplorer::HeaderPath::FrameworkHeaderPath
                                    ? ProjectPart::HeaderPath::FrameworkPath
                                    : ProjectPart::HeaderPath::IncludePath);
    }

115
    toolchainDefines = tc->predefinedMacros(commandLineFlags);
116
    toolchainType = tc->typeId();
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    updateLanguageFeatures();
}

void ProjectPart::updateLanguageFeatures()
{
    const bool hasQt = qtVersion != NoQt;
    languageFeatures.cxx11Enabled = languageVersion >= CXX11;
    languageFeatures.qtEnabled = hasQt;
    languageFeatures.qtMocRunEnabled = hasQt;
    if (!hasQt) {
        languageFeatures.qtKeywordsEnabled = false;
    } else {
        const QByteArray noKeywordsMacro = "#define QT_NO_KEYWORDS";
        const int noKeywordsIndex = projectDefines.indexOf(noKeywordsMacro);
        if (noKeywordsIndex == -1) {
            languageFeatures.qtKeywordsEnabled = true;
        } else {
            const char nextChar = projectDefines.at(noKeywordsIndex + noKeywordsMacro.length());
            // Detect "#define QT_NO_KEYWORDS" and "#define QT_NO_KEYWORDS 1", but exclude
            // "#define QT_NO_KEYWORDS_FOO"
            languageFeatures.qtKeywordsEnabled = nextChar != '\n' && nextChar != ' ';
        }
    }
140 141
}

142 143 144 145 146
ProjectPart::Ptr ProjectPart::copy() const
{
    return Ptr(new ProjectPart(*this));
}

147 148
QString ProjectPart::id() const
{
149 150 151 152
    QString projectPartId = QDir::fromNativeSeparators(projectFile);
    if (!displayName.isEmpty())
        projectPartId.append(QLatin1Char(' ') + displayName);
    return projectPartId;
153 154
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
QByteArray ProjectPart::readProjectConfigFile(const ProjectPart::Ptr &part)
{
    QByteArray result;

    QFile f(part->projectConfigFile);
    if (f.open(QIODevice::ReadOnly)) {
        QTextStream is(&f);
        result = is.readAll().toUtf8();
        f.close();
    }

    return result;
}

ProjectInfo::ProjectInfo()
{}

172
ProjectInfo::ProjectInfo(QPointer<Project> project)
173 174 175
    : m_project(project)
{}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
bool ProjectInfo::operator ==(const ProjectInfo &other) const
{
    return m_project == other.m_project
            && m_projectParts == other.m_projectParts
            && m_compilerCallData == other.m_compilerCallData
            && m_headerPaths == other.m_headerPaths
            && m_sourceFiles == other.m_sourceFiles
            && m_defines == other.m_defines;
}

bool ProjectInfo::operator !=(const ProjectInfo &other) const
{
    return !operator ==(other);
}

bool ProjectInfo::definesChanged(const ProjectInfo &other) const
{
    return m_defines != other.m_defines;
}

bool ProjectInfo::configurationChanged(const ProjectInfo &other) const
{
    return definesChanged(other) || m_headerPaths != other.m_headerPaths;
}

bool ProjectInfo::configurationOrFilesChanged(const ProjectInfo &other) const
{
    return configurationChanged(other) || m_sourceFiles != other.m_sourceFiles;
}

206 207 208 209 210
bool ProjectInfo::isValid() const
{
    return !m_project.isNull();
}

211
QPointer<Project> ProjectInfo::project() const
212 213 214 215 216 217 218 219 220 221 222
{
    return m_project;
}

const QList<ProjectPart::Ptr> ProjectInfo::projectParts() const
{
    return m_projectParts;
}

void ProjectInfo::appendProjectPart(const ProjectPart::Ptr &part)
{
223 224 225
    if (part)
        m_projectParts.append(part);
}
226

227 228
void ProjectInfo::finish()
{
229 230
    typedef ProjectPart::HeaderPath HeaderPath;

231 232
    QSet<HeaderPath> incs;
    foreach (const ProjectPart::Ptr &part, m_projectParts) {
233
        part->updateLanguageFeatures();
234 235 236 237 238 239
        // Update header paths
        foreach (const HeaderPath &hp, part->headerPaths) {
            if (!incs.contains(hp)) {
                incs.insert(hp);
                m_headerPaths += hp;
            }
240 241
        }

242 243 244 245 246 247 248 249 250 251 252 253
        // Update source files
        foreach (const ProjectFile &file, part->files)
            m_sourceFiles.insert(file.path);

        // Update defines
        m_defines.append(part->toolchainDefines);
        m_defines.append(part->projectDefines);
        if (!part->projectConfigFile.isEmpty()) {
            m_defines.append('\n');
            m_defines += ProjectPart::readProjectConfigFile(part);
            m_defines.append('\n');
        }
254 255 256 257 258 259 260 261
    }
}

const ProjectPart::HeaderPaths ProjectInfo::headerPaths() const
{
    return m_headerPaths;
}

262
const QSet<QString> ProjectInfo::sourceFiles() const
263 264 265 266 267 268 269 270 271
{
    return m_sourceFiles;
}

const QByteArray ProjectInfo::defines() const
{
    return m_defines;
}

272 273 274 275 276 277 278 279 280 281
void ProjectInfo::setCompilerCallData(const CompilerCallData &data)
{
    m_compilerCallData = data;
}

ProjectInfo::CompilerCallData ProjectInfo::compilerCallData() const
{
    return m_compilerCallData;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
namespace {
class ProjectFileCategorizer
{
public:
    ProjectFileCategorizer(const QString &partName, const QStringList &files)
        : m_partName(partName)
    {
        using CppTools::ProjectFile;

        QStringList cHeaders, cxxHeaders;

        foreach (const QString &file, files) {
            switch (ProjectFile::classify(file)) {
            case ProjectFile::CSource: m_cSources += file; break;
            case ProjectFile::CHeader: cHeaders += file; break;
            case ProjectFile::CXXSource: m_cxxSources += file; break;
            case ProjectFile::CXXHeader: cxxHeaders += file; break;
            case ProjectFile::ObjCSource: m_objcSources += file; break;
            case ProjectFile::ObjCXXSource: m_objcxxSources += file; break;
            default:
                continue;
            }
        }

        const bool hasC = !m_cSources.isEmpty();
        const bool hasCxx = !m_cxxSources.isEmpty();
        const bool hasObjc = !m_objcSources.isEmpty();
        const bool hasObjcxx = !m_objcxxSources.isEmpty();

        if (hasObjcxx)
            m_objcxxSources += cxxHeaders + cHeaders;
        if (hasCxx)
            m_cxxSources += cxxHeaders + cHeaders;
        else if (!hasObjcxx)
            m_cxxSources += cxxHeaders;
        if (hasObjc)
            m_objcSources += cHeaders;
        if (hasC || (!hasObjc && !hasObjcxx && !hasCxx))
            m_cSources += cHeaders;

        m_partCount =
                (m_cSources.isEmpty() ? 0 : 1) +
                (m_cxxSources.isEmpty() ? 0 : 1) +
                (m_objcSources.isEmpty() ? 0 : 1) +
                (m_objcxxSources.isEmpty() ? 0 : 1);
    }

    bool hasCSources() const { return !m_cSources.isEmpty(); }
    bool hasCxxSources() const { return !m_cxxSources.isEmpty(); }
    bool hasObjcSources() const { return !m_objcSources.isEmpty(); }
    bool hasObjcxxSources() const { return !m_objcxxSources.isEmpty(); }

    QStringList cSources() const { return m_cSources; }
    QStringList cxxSources() const { return m_cxxSources; }
    QStringList objcSources() const { return m_objcSources; }
    QStringList objcxxSources() const { return m_objcxxSources; }

    bool hasMultipleParts() const { return m_partCount > 1; }
    bool hasNoParts() const { return m_partCount == 0; }

    QString partName(const QString &languageName) const
    {
        if (hasMultipleParts())
            return QString::fromLatin1("%1 (%2)").arg(m_partName).arg(languageName);

        return m_partName;
    }

private:
    QString m_partName;
    QStringList m_cSources, m_cxxSources, m_objcSources, m_objcxxSources;
    int m_partCount;
};
} // anonymous namespace

ProjectPartBuilder::ProjectPartBuilder(ProjectInfo &pInfo)
    : m_templatePart(new ProjectPart)
    , m_pInfo(pInfo)
{
    m_templatePart->project = pInfo.project();
    m_templatePart->displayName = pInfo.project()->displayName();
    m_templatePart->projectFile = pInfo.project()->projectFilePath().toString();
}

void ProjectPartBuilder::setQtVersion(ProjectPart::QtVersion qtVersion)
{
    m_templatePart->qtVersion = qtVersion;
}

void ProjectPartBuilder::setCFlags(const QStringList &flags)
{
    m_cFlags = flags;
}

void ProjectPartBuilder::setCxxFlags(const QStringList &flags)
{
    m_cxxFlags = flags;
}

void ProjectPartBuilder::setDefines(const QByteArray &defines)
{
    m_templatePart->projectDefines = defines;
}

void ProjectPartBuilder::setHeaderPaths(const ProjectPart::HeaderPaths &headerPaths)
{
    m_templatePart->headerPaths = headerPaths;
}

void ProjectPartBuilder::setIncludePaths(const QStringList &includePaths)
{
    m_templatePart->headerPaths.clear();

    foreach (const QString &includeFile, includePaths) {
        ProjectPart::HeaderPath hp(includeFile, ProjectPart::HeaderPath::IncludePath);

        // The simple project managers are utterly ignorant of frameworks on OSX, and won't report
        // framework paths. The work-around is to check if the include path ends in ".framework",
        // and if so, add the parent directory as framework path.
        if (includeFile.endsWith(QLatin1String(".framework"))) {
            const int slashIdx = includeFile.lastIndexOf(QLatin1Char('/'));
            if (slashIdx != -1) {
                hp = ProjectPart::HeaderPath(includeFile.left(slashIdx),
                                             ProjectPart::HeaderPath::FrameworkPath);
            }
        }

        m_templatePart->headerPaths += hp;
    }
}

void ProjectPartBuilder::setPreCompiledHeaders(const QStringList &pchs)
{
    m_templatePart->precompiledHeaders = pchs;
}

void ProjectPartBuilder::setProjectFile(const QString &projectFile)
{
    m_templatePart->projectFile = projectFile;
}

void ProjectPartBuilder::setDisplayName(const QString &displayName)
{
    m_templatePart->displayName = displayName;
}

void ProjectPartBuilder::setConfigFileName(const QString &configFileName)
{
    m_templatePart->projectConfigFile = configFileName;
}

QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList &files)
{
    QList<Core::Id> languages;

    ProjectFileCategorizer cat(m_templatePart->displayName, files);
    if (cat.hasNoParts())
        return languages;

    using CppTools::ProjectFile;
    using CppTools::ProjectPart;

    if (cat.hasCSources()) {
        createProjectPart(cat.cSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "C11")),
                          ProjectPart::C11,
448
                          ProjectPart::NoExtensions);
449 450 451 452 453 454 455
        // TODO: there is no C...
//        languages += ProjectExplorer::Constants::LANG_C;
    }
    if (cat.hasObjcSources()) {
        createProjectPart(cat.objcSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "Obj-C11")),
                          ProjectPart::C11,
456
                          ProjectPart::ObjectiveCExtensions);
457 458 459 460 461 462
        // TODO: there is no Ojective-C...
//        languages += ProjectExplorer::Constants::LANG_OBJC;
    }
    if (cat.hasCxxSources()) {
        createProjectPart(cat.cxxSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "C++11")),
463 464
                          ProjectPart::CXX11,
                          ProjectPart::NoExtensions);
465 466 467 468 469
        languages += ProjectExplorer::Constants::LANG_CXX;
    }
    if (cat.hasObjcxxSources()) {
        createProjectPart(cat.objcxxSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "Obj-C++11")),
470 471
                          ProjectPart::CXX11,
                          ProjectPart::ObjectiveCExtensions);
472 473 474 475 476 477 478 479 480
        // TODO: there is no Objective-C++...
        languages += ProjectExplorer::Constants::LANG_CXX;
    }

    return languages;
}

void ProjectPartBuilder::createProjectPart(const QStringList &theSources,
                                           const QString &partName,
481 482
                                           ProjectPart::LanguageVersion languageVersion,
                                           ProjectPart::LanguageExtensions languageExtensions)
483
{
484
    ProjectPart::Ptr part(m_templatePart->copy());
485 486
    part->displayName = partName;

487
    QTC_ASSERT(part->project, return);
488
    if (Target *activeTarget = part->project->activeTarget()) {
489 490 491 492 493 494 495 496
        if (Kit *kit = activeTarget->kit()) {
            if (ToolChain *toolChain = ToolChainKitInformation::toolChain(kit)) {
                const QStringList flags = languageVersion >= ProjectPart::CXX98 ? m_cxxFlags
                                                                                : m_cFlags;
                part->evaluateToolchain(toolChain, flags, SysRootKitInformation::sysRoot(kit));
            }
        }
    }
497

498
    part->languageExtensions |= languageExtensions;
499

500
    ProjectFileAdder adder(part->files);
501 502 503 504 505
    foreach (const QString &file, theSources)
        adder.maybeAdd(file);

    m_pInfo.appendProjectPart(part);
}
506 507


508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
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);
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
QString CompilerOptionsBuilder::defineLineToDefineOption(const QByteArray &defineLine)
{
    QByteArray str = defineLine.mid(8);
    int spaceIdx = str.indexOf(' ');
    const QString option = defineOption();
    const bool hasValue = spaceIdx != -1;
    QString arg = option + QLatin1String(str.left(hasValue ? spaceIdx : str.size()) + '=');
    if (hasValue)
        arg += QLatin1String(str.mid(spaceIdx + 1));
    return arg;
}

void CompilerOptionsBuilder::addDefine(const QByteArray &defineLine)
{
    m_options.append(defineLineToDefineOption(defineLine));
}

void CompilerOptionsBuilder::addHeaderPathOptions()
541 542
{
    typedef ProjectPart::HeaderPath HeaderPath;
543
    const QString defaultPrefix = includeOption();
544 545 546

    QStringList result;

547
    foreach (const HeaderPath &headerPath , m_projectPart->headerPaths) {
548 549 550
        if (headerPath.path.isEmpty())
            continue;

551
        if (excludeHeaderPath(headerPath.path))
552 553 554 555 556 557 558 559 560 561
            continue;

        QString prefix;
        switch (headerPath.type) {
        case HeaderPath::FrameworkPath:
            prefix = QLatin1String("-F");
            break;
        default: // This shouldn't happen, but let's be nice..:
            // intentional fall-through:
        case HeaderPath::IncludePath:
562
            prefix = defaultPrefix;
563 564 565 566 567 568
            break;
        }

        result.append(prefix + headerPath.path);
    }

569
    m_options.append(result);
570 571
}

572
void CompilerOptionsBuilder::addToolchainAndProjectDefines()
573
{
574
    QByteArray extendedDefines = m_projectPart->toolchainDefines + m_projectPart->projectDefines;
575 576
    QStringList result;

577
    foreach (QByteArray def, extendedDefines.split('\n')) {
578
        if (def.isEmpty() || excludeDefineLine(def))
579 580
            continue;

581 582 583
        const QString defineOption = defineLineToDefineOption(def);
        if (!result.contains(defineOption))
            result.append(defineOption);
584 585
    }

586
    m_options.append(result);
587 588
}

589
static QStringList createLanguageOptionGcc(ProjectFile::Kind fileKind, bool objcExt)
590 591 592 593
{
    QStringList opts;

    switch (fileKind) {
594 595
    case ProjectFile::Unclassified:
        break;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    case ProjectFile::CHeader:
        if (objcExt)
            opts += QLatin1String("objective-c-header");
        else
            opts += QLatin1String("c-header");
        break;

    case ProjectFile::CXXHeader:
    default:
        if (!objcExt) {
            opts += QLatin1String("c++-header");
            break;
        } // else: fall-through!
    case ProjectFile::ObjCHeader:
    case ProjectFile::ObjCXXHeader:
        opts += QLatin1String("objective-c++-header");
        break;

    case ProjectFile::CSource:
        if (!objcExt) {
            opts += QLatin1String("c");
            break;
        } // else: fall-through!
    case ProjectFile::ObjCSource:
        opts += QLatin1String("objective-c");
        break;

    case ProjectFile::CXXSource:
        if (!objcExt) {
            opts += QLatin1String("c++");
            break;
        } // else: fall-through!
    case ProjectFile::ObjCXXSource:
        opts += QLatin1String("objective-c++");
        break;

    case ProjectFile::OpenCLSource:
        opts += QLatin1String("cl");
        break;
    case ProjectFile::CudaSource:
        opts += QLatin1String("cuda");
        break;
    }

640 641 642
    if (!opts.isEmpty())
        opts.prepend(QLatin1String("-x"));

643 644 645
    return opts;
}

646
void CompilerOptionsBuilder::addLanguageOption(ProjectFile::Kind fileKind)
647
{
648
    const bool objcExt = m_projectPart->languageExtensions & ProjectPart::ObjectiveCExtensions;
649
    const QStringList options = createLanguageOptionGcc(fileKind, objcExt);
650
    m_options.append(options);
651 652
}

653
void CompilerOptionsBuilder::addOptionsForLanguage(bool checkForBorlandExtensions)
654 655
{
    QStringList opts;
656 657 658
    const ProjectPart::LanguageExtensions languageExtensions = m_projectPart->languageExtensions;
    const bool gnuExtensions = languageExtensions & ProjectPart::GnuExtensions;
    switch (m_projectPart->languageVersion) {
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
    case ProjectPart::C89:
        opts << (gnuExtensions ? QLatin1String("-std=gnu89") : QLatin1String("-std=c89"));
        break;
    case ProjectPart::C99:
        opts << (gnuExtensions ? QLatin1String("-std=gnu99") : QLatin1String("-std=c99"));
        break;
    case ProjectPart::C11:
        opts << (gnuExtensions ? QLatin1String("-std=gnu11") : QLatin1String("-std=c11"));
        break;
    case ProjectPart::CXX11:
        opts << (gnuExtensions ? QLatin1String("-std=gnu++11") : QLatin1String("-std=c++11"));
        break;
    case ProjectPart::CXX98:
        opts << (gnuExtensions ? QLatin1String("-std=gnu++98") : QLatin1String("-std=c++98"));
        break;
    case ProjectPart::CXX03:
675
        // Clang 3.6 does not know -std=gnu++03.
676 677 678
        opts << QLatin1String("-std=c++03");
        break;
    case ProjectPart::CXX14:
679
        opts << (gnuExtensions ? QLatin1String("-std=gnu++14") : QLatin1String("-std=c++14"));
680 681
        break;
    case ProjectPart::CXX17:
682 683
        // TODO: Change to (probably) "gnu++17"/"c++17" at some point in the future.
        opts << (gnuExtensions ? QLatin1String("-std=gnu++1z") : QLatin1String("-std=c++1z"));
684 685 686 687 688 689 690 691 692
        break;
    }

    if (languageExtensions & ProjectPart::MicrosoftExtensions)
        opts << QLatin1String("-fms-extensions");

    if (checkForBorlandExtensions && (languageExtensions & ProjectPart::BorlandExtensions))
        opts << QLatin1String("-fborland-extensions");

693
    m_options.append(opts);
694
}
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

QString CompilerOptionsBuilder::includeOption() const
{
    return QLatin1String("-I");
}

QString CompilerOptionsBuilder::defineOption() const
{
    return QLatin1String("-D");
}

bool CompilerOptionsBuilder::excludeDefineLine(const QByteArray &defineLine) const
{
    // This is a quick fix for QTCREATORBUG-11501.
    // TODO: do a proper fix, see QTCREATORBUG-11709.
    if (defineLine.startsWith("#define __cplusplus"))
        return true;

713 714 715 716 717 718
    // gcc 4.9 has:
    //    #define __has_include(STR) __has_include__(STR)
    //    #define __has_include_next(STR) __has_include_next__(STR)
    // The right-hand sides are gcc built-ins that clang does not understand, and they'd
    // override clang's own (non-macro, it seems) definitions of the symbols on the left-hand
    // side.
719
    const bool isGccToolchain = m_projectPart->toolchainType == ProjectExplorer::Constants::GCC_TOOLCHAIN_TYPEID;
720 721 722
    if (isGccToolchain && defineLine.contains("has_include"))
        return true;

723 724 725 726 727 728 729 730
    return false;
}

bool CompilerOptionsBuilder::excludeHeaderPath(const QString &headerPath) const
{
    Q_UNUSED(headerPath);
    return false;
}