From kde-commits Thu Mar 31 19:30:37 2016 From: Alex Richardson Date: Thu, 31 Mar 2016 19:30:37 +0000 To: kde-commits Subject: [kdevelop/5.0] languages/plugins/custom-definesandincludes/compilerprovider: Correctly Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=145945264732330 Git commit 52346836e143e0fec064092993252d6d51679263 by Alex Richardson. Committed on 31/03/2016 at 19:30. Pushed by arichardson into branch '5.0'. Correctly determine language for more -std=3D flags In particular we now support e.g. gnu++1z for C++ as well as gnu99 or iso9899:1999 and similar for C. This means that the -imacros file will no longer define __cplusplus when parsing C code. We also print a warning message now we couldn't determine the language from the -std=3D flag Test Plan: My files that are compiled with -std=3D=3Diso9899:1999 show errors about nullptr not being known. This happened before because __cplusplus was defined and NULL was therefore defined to nullptr Reviewers: mwolff, kfunk Reviewed By: kfunk Subscribers: kdevelop-devel Differential Revision: https://phabricator.kde.org/D1277 M +30 -4 languages/plugins/custom-definesandincludes/compilerprovider= /gcclikecompiler.cpp http://commits.kde.org/kdevelop/52346836e143e0fec064092993252d6d51679263 diff --git a/languages/plugins/custom-definesandincludes/compilerprovider/g= cclikecompiler.cpp b/languages/plugins/custom-definesandincludes/compilerpr= ovider/gcclikecompiler.cpp index 525700c..bf8784c 100644 --- a/languages/plugins/custom-definesandincludes/compilerprovider/gcclikec= ompiler.cpp +++ b/languages/plugins/custom-definesandincludes/compilerprovider/gcclikec= ompiler.cpp @@ -34,18 +34,43 @@ using namespace KDevelop; = namespace { +// compilers don't deduplicate QStringLiteral +QString minusXC() { return QStringLiteral("-xc"); } +QString minusXCPlusPlus() { return QStringLiteral("-xc++"); } + QStringList languageOptions(const QString& arguments) { - const QRegularExpression regexp("-std=3D(c|c\\+\\+)[0-9]{2}"); = + // TODO: handle -ansi flag: In C mode, this is equivalent to -std=3Dc9= 0. In C++ mode, it is equivalent to -std=3Dc++98. + // TODO: check for -x flag on command line + const QRegularExpression regexp("-std=3D(\\S+)"); + // see gcc manpage or llvm/tools/clang/include/clang/Frontend/LangStan= dards.def for list of valid language options auto result =3D regexp.match(arguments); if(result.hasMatch()){ auto standard =3D result.captured(0); - auto language =3D result.captured(1) =3D=3D QStringLiteral("c++") = ? QStringLiteral("-xc++") : QStringLiteral("-xc"); + QString mode =3D result.captured(1); + QString language; + if (mode.startsWith(QLatin1String("c++")) || mode.startsWith(QLati= n1String("gnu++"))) { + language =3D minusXCPlusPlus(); + } else if (mode.startsWith(QLatin1String("iso9899:"))) { + // all iso9899:xxxxx modes are C standards + language =3D minusXC(); + } else { + // check for c11, gnu99, etc: all of them have a digit after t= he c/gnu + const QRegularExpression cRegexp("(c|gnu)\\d.*"); + if (cRegexp.match(mode).hasMatch()) { + language =3D minusXC(); + } + } + if (language.isEmpty()) { + qCWarning(DEFINESANDINCLUDES) << "Failed to determine language= from -std=3D flag:" << arguments; + language =3D minusXCPlusPlus(); + } return {standard, language}; - } = - return {QStringLiteral("--std=3Dc++11"), QStringLiteral("-xc++")}; + } + // no -std=3D flag passed -> assume c++11 + return {QStringLiteral("-std=3Dc++11"), minusXCPlusPlus()}; } = } @@ -63,6 +88,7 @@ Defines GccLikeCompiler::defines(const QString& arguments= ) const QProcess proc; proc.setProcessChannelMode( QProcess::MergedChannels ); = + // TODO: what about -mXXX or -target=3D flags, some of these change se= arch paths/defines auto compilerArguments =3D languageOptions(arguments); compilerArguments.append("-dM"); compilerArguments.append("-E");