clangutils.cpp 12.2 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
** Contact: http://www.qt-project.org/legal
**
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "clangutils.h"

#include <clang-c/Index.h>

#include <coreplugin/documentmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>

38 39
#include <cpptools/cppworkingcopy.h>

40
#include <QDir>
41 42 43 44 45 46 47 48 49
#include <QFile>
#include <QSet>
#include <QString>

using namespace ClangCodeModel;
using namespace ClangCodeModel::Internal;
using namespace Core;
using namespace CppTools;

50
static const bool BeVerbose = qgetenv("QTC_CLANG_VERBOSE") == "1";
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
namespace ClangCodeModel {
namespace Utils {

namespace {
bool isBlacklisted(const QString &path)
{
    static QStringList blacklistedPaths = QStringList()
            << QLatin1String("lib/gcc/i686-apple-darwin");

    foreach (const QString &blacklisted, blacklistedPaths)
        if (path.contains(blacklisted))
            return true;

    return false;
}
} // anonymous namespace

69
UnsavedFiles createUnsavedFiles(WorkingCopy workingCopy)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
    // TODO: change the modelmanager to hold one working copy, and amend it every time we ask for one.
    // TODO: Reason: the UnsavedFile needs a QByteArray.

    QSet<QString> modifiedFiles;
    foreach (IDocument *doc, Core::DocumentManager::modifiedDocuments())
        modifiedFiles.insert(doc->filePath());

    UnsavedFiles result;
    QHashIterator<QString, QPair<QByteArray, unsigned> > wcIter = workingCopy.iterator();
    while (wcIter.hasNext()) {
        wcIter.next();
        const QString &fileName = wcIter.key();
        if (modifiedFiles.contains(fileName) && QFile(fileName).exists())
            result.insert(fileName, wcIter.value().first);
    }

    return result;
}

/**
 * @brief Creates list of command-line arguments required for correct parsing
 * @param pPart Null if file isn't part of any project
 * @param fileName Path to file, non-empty
 */
QStringList createClangOptions(const ProjectPart::Ptr &pPart, const QString &fileName)
{
    ProjectFile::Kind fileKind = ProjectFile::Unclassified;
    if (!pPart.isNull())
        foreach (const ProjectFile &file, pPart->files)
            if (file.path == fileName) {
                fileKind = file.kind;
                break;
            }
    if (fileKind == ProjectFile::Unclassified)
        fileKind = ProjectFile::classify(fileName);

    return createClangOptions(pPart, fileKind);
}

static QStringList buildDefines(const QByteArray &defines, bool toolchainDefines)
{
    QStringList result;

    foreach (QByteArray def, defines.split('\n')) {
        if (def.isEmpty())
            continue;

118 119 120 121 122
        // This is a quick fix for QTCREATORBUG-11501.
        // TODO: do a proper fix, see QTCREATORBUG-11709.
        if (def.startsWith("#define __cplusplus"))
            continue;

123
        // TODO: verify if we can pass compiler-defined macros when also passing -undef.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        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;
        }

        QByteArray str = def.mid(8);
        int spaceIdx = str.indexOf(' ');
        QString arg;
        if (spaceIdx != -1) {
            arg = QLatin1String("-D" + str.left(spaceIdx) + "=" + str.mid(spaceIdx + 1));
        } else {
            arg = QLatin1String("-D" + str);
        }
        arg = arg.replace(QLatin1String("\\\""), QLatin1String("\""));
        arg = arg.replace(QLatin1String("\""), QLatin1String(""));
        if (!result.contains(arg))
            result.append(arg);
    }

    return result;
}

151 152 153 154 155 156 157 158 159
static QString getResourceDir()
{
    QDir dir(Core::ICore::instance()->resourcePath() + QLatin1String("/cplusplus/clang/") +
             QLatin1String(CLANG_VERSION) + QLatin1String("/include"));
    if (!dir.exists() || !QFileInfo(dir, QLatin1String("stdint.h")).exists())
        dir = QDir(QLatin1String(CLANG_RESOURCE_DIR));
    return dir.canonicalPath();
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * @brief Creates list of command-line arguments required for correct parsing
 * @param pPart Null if file isn't part of any project
 * @param fileKind Determines language and source/header state
 */
QStringList createClangOptions(const ProjectPart::Ptr &pPart, ProjectFile::Kind fileKind)
{
    QStringList result = clangLanguageOption(fileKind);
    switch (fileKind) {
    default:
    case CppTools::ProjectFile::CXXHeader:
    case CppTools::ProjectFile::CXXSource:
    case CppTools::ProjectFile::ObjCXXHeader:
    case CppTools::ProjectFile::ObjCXXSource:
    case CppTools::ProjectFile::CudaSource:
    case CppTools::ProjectFile::OpenCLSource:
        result << clangOptionsForCxx(pPart->qtVersion,
                                     pPart->cxxVersion,
                                     pPart->cxxExtensions);
        break;
    case CppTools::ProjectFile::CHeader:
    case CppTools::ProjectFile::CSource:
    case CppTools::ProjectFile::ObjCHeader:
    case CppTools::ProjectFile::ObjCSource:
        result << clangOptionsForC(pPart->cVersion,
                                   pPart->cxxExtensions);
        break;
    }

    if (pPart.isNull())
        return result;

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    static const QString resourceDir = getResourceDir();

    if (BeVerbose)
        result << QLatin1String("-v");

    if (!resourceDir.isEmpty()) {
        result << QLatin1String("-nostdlibinc");
        result << (QLatin1String("-I") + resourceDir);
        result << QLatin1String("-undef");
    }

    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");
207

208 209 210
    if (!pPart->projectConfigFile.isEmpty())
        result << QLatin1String("-include") << pPart->projectConfigFile;

211
    result << buildDefines(pPart->toolchainDefines, false);
212 213
    result << buildDefines(pPart->projectDefines, false);

214 215 216 217 218 219 220
    typedef ProjectPart::HeaderPath HeaderPath;
    foreach (const HeaderPath &headerPath , pPart->headerPaths) {
        if (headerPath.path.isEmpty() || isBlacklisted(headerPath.path))
            continue;

        QString prefix;
        switch (headerPath.type) {
Erik Verbruggen's avatar
Erik Verbruggen committed
221 222 223 224 225 226 227 228
        case HeaderPath::FrameworkPath:
            prefix = QLatin1String("-F");
            break;
        default: // This shouldn't happen, but let's be nice..:
            // intentional fall-through:
        case HeaderPath::IncludePath:
            prefix = QLatin1String("-I");
            break;
229 230 231 232
        }

        result.append(prefix + headerPath.path);
    }
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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

#if 0
    qDebug() << "--- m_args:";
    foreach (const QString &arg, result)
        qDebug() << "\t" << qPrintable(arg);
    qDebug() << "---";
#endif

    return result;
}

/// @return Option to speed up parsing with precompiled header
QStringList createPCHInclusionOptions(const QStringList &pchFiles)
{
    QStringList opts;

    foreach (const QString &pchFile, pchFiles) {
        if (QFile(pchFile).exists()) {
            opts += QLatin1String("-include-pch");
            opts += pchFile;
        }
    }

    return opts;
}

/// @return Option to speed up parsing with precompiled header
QStringList createPCHInclusionOptions(const QString &pchFile)
{
    return createPCHInclusionOptions(QStringList() << pchFile);
}

/// @return "-std" flag to select standard, flags for C extensions
QStringList clangOptionsForC(ProjectPart::CVersion cVersion, ProjectPart::CXXExtensions cxxExtensions)
{
    QStringList opts;
    bool gnuExpensions = cxxExtensions & ProjectPart::GnuExtensions;
    switch (cVersion) {
    case ProjectPart::C89:
        opts << (gnuExpensions ? QLatin1String("-std=gnu89") : QLatin1String("-std=c89"));
        break;
    case ProjectPart::C99:
        opts << (gnuExpensions ? QLatin1String("-std=gnu99") : QLatin1String("-std=c99"));
        break;
    case ProjectPart::C11:
        opts << (gnuExpensions ? QLatin1String("-std=gnu11") : QLatin1String("-std=c11"));
        break;
    }

    if (cxxExtensions & ProjectPart::MicrosoftExtensions) {
        opts << QLatin1String("-fms-extensions");
    }

#if defined(CINDEX_VERSION) // clang 3.2 or higher
    if (cxxExtensions & ProjectPart::BorlandExtensions)
        opts << QLatin1String("-fborland-extensions");
#endif

    return opts;
}

/// @return "-std" flag to select standard, flags for C++ extensions, Qt injections
QStringList clangOptionsForCxx(ProjectPart::QtVersion qtVersion,
                                      ProjectPart::CXXVersion cxxVersion,
                                      ProjectPart::CXXExtensions cxxExtensions)
{
    QStringList opts;
    bool gnuExpensions = cxxExtensions & ProjectPart::GnuExtensions;
    switch (cxxVersion) {
    case ProjectPart::CXX11:
        opts << (gnuExpensions ? QLatin1String("-std=gnu++11") : QLatin1String("-std=c++11"));
        break;
    case ProjectPart::CXX98:
        opts << (gnuExpensions ? QLatin1String("-std=gnu++98") : QLatin1String("-std=c++98"));
        break;
    }

    if (cxxExtensions & ProjectPart::MicrosoftExtensions) {
        opts << QLatin1String("-fms-extensions")
             << QLatin1String("-fdelayed-template-parsing");
    }

#if defined(CINDEX_VERSION) // clang 3.2 or higher
    if (cxxExtensions & ProjectPart::BorlandExtensions)
        opts << QLatin1String("-fborland-extensions");
#endif

    static const QString injectedHeader(Core::ICore::instance()->resourcePath() + QLatin1String("/cplusplus/qt%1-qobjectdefs-injected.h"));
//    if (qtVersion == ProjectPart::Qt4)
//        opts << QLatin1String("-include") << injectedHeader.arg(QLatin1Char('4'));
    if (qtVersion == ProjectPart::Qt5)
        opts << QLatin1String("-include") << injectedHeader.arg(QLatin1Char('5'));

    return opts;
}

/// @return "-x language-code"
QStringList clangLanguageOption(ProjectFile::Kind fileKind)
{
    QStringList opts;
    opts += QLatin1String("-x");

    switch (fileKind) {
    case ProjectFile::CHeader:
//        opts += QLatin1String("c-header");
//        break;
    case ProjectFile::CXXHeader:
    default:
        opts += QLatin1String("c++-header");
        break;
    case ProjectFile::CXXSource:
        opts += QLatin1String("c++");
        break;
    case ProjectFile::CSource:
        opts += QLatin1String("c");
        break;
    case ProjectFile::ObjCHeader:
//        opts += QLatin1String("objective-c-header");
//        break;
    case ProjectFile::ObjCXXHeader:
        opts += QLatin1String("objective-c++-header");
        break;
    case ProjectFile::ObjCSource:
        opts += QLatin1String("objective-c");
        break;
    case ProjectFile::ObjCXXSource:
        opts += QLatin1String("objective-c++");
        break;
    case ProjectFile::OpenCLSource:
        opts += QLatin1String("cl");
        break;
    case ProjectFile::CudaSource:
        opts += QLatin1String("cuda");
        break;
    }

    return opts;
}

} // namespace Utils
} // namespace Clang