C++: Disable C++ keywords in C files

In some (legacy) C files, new and delete may be used for regular identifier.

There are some limitations:
* Header files have no 'implicit' type, and may be parsed as C++ or ObjC depending on the
other files in the project.
* QMakeProject use a single ProjectPart for C and C++ files, so there will still be the issue.

Change-Id: Iec11687b35f7ccf1e7c0d091b143ae90d950e440
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Francois Ferrand
2016-02-26 18:48:39 +01:00
parent 285c8a7543
commit 7b2c09a118
11 changed files with 53 additions and 37 deletions

View File

@@ -61,14 +61,14 @@ static inline int classify3(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'n') {
else if (features.cxxEnabled && s[0] == 'n') {
if (s[1] == 'e') {
if (s[2] == 'w') {
return T_NEW;
}
}
}
else if (s[0] == 't') {
else if (features.cxxEnabled && s[0] == 't') {
if (s[1] == 'r') {
if (s[2] == 'y') {
return T_TRY;
@@ -90,7 +90,7 @@ static inline int classify3(const char *s, LanguageFeatures features)
static inline int classify4(const char *s, LanguageFeatures features)
{
if (s[0] == 'a') {
if (features.cxxEnabled && s[0] == 'a') {
if (s[1] == 'u') {
if (s[2] == 't') {
if (s[3] == 'o') {
@@ -166,7 +166,7 @@ static inline int classify4(const char *s, LanguageFeatures features)
}
}
else if (s[0] == 't') {
if (s[1] == 'h') {
if (features.cxxEnabled && s[1] == 'h') {
if (s[2] == 'i') {
if (s[3] == 's') {
return T_THIS;
@@ -227,7 +227,7 @@ static inline int classify5(const char *s, LanguageFeatures features)
}
}
else if (s[0] == 'c') {
if (s[1] == 'a') {
if (features.cxxEnabled && s[1] == 'a') {
if (s[2] == 't') {
if (s[3] == 'c') {
if (s[4] == 'h') {
@@ -236,7 +236,7 @@ static inline int classify5(const char *s, LanguageFeatures features)
}
}
}
else if (s[1] == 'l') {
else if (features.cxxEnabled && s[1] == 'l') {
if (s[2] == 'a') {
if (s[3] == 's') {
if (s[4] == 's') {
@@ -297,7 +297,7 @@ static inline int classify5(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 't') {
else if (features.cxxEnabled && s[0] == 't') {
if (s[1] == 'h') {
if (s[2] == 'r') {
if (s[3] == 'o') {
@@ -318,7 +318,7 @@ static inline int classify5(const char *s, LanguageFeatures features)
}
}
}
else if (s[1] == 's') {
else if (features.cxxEnabled && s[1] == 's') {
if (s[2] == 'i') {
if (s[3] == 'n') {
if (s[4] == 'g') {
@@ -345,7 +345,7 @@ static inline int classify5(const char *s, LanguageFeatures features)
static inline int classify6(const char *s, LanguageFeatures features)
{
if (s[0] == 'd') {
if (s[1] == 'e') {
if (features.cxxEnabled && s[1] == 'e') {
if (s[2] == 'l') {
if (s[3] == 'e') {
if (s[4] == 't') {
@@ -370,7 +370,7 @@ static inline int classify6(const char *s, LanguageFeatures features)
}
else if (s[0] == 'e') {
if (s[1] == 'x') {
if (s[2] == 'p') {
if (features.cxxEnabled && s[2] == 'p') {
if (s[3] == 'o') {
if (s[4] == 'r') {
if (s[5] == 't') {
@@ -390,7 +390,7 @@ static inline int classify6(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'f') {
else if (features.cxxEnabled && s[0] == 'f') {
if (s[1] == 'r') {
if (s[2] == 'i') {
if (s[3] == 'e') {
@@ -416,7 +416,7 @@ static inline int classify6(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'p') {
else if (features.cxxEnabled && s[0] == 'p') {
if (s[1] == 'u') {
if (s[2] == 'b') {
if (s[3] == 'l') {
@@ -499,7 +499,7 @@ static inline int classify6(const char *s, LanguageFeatures features)
if (s[1] == 'y') {
if (s[2] == 'p') {
if (s[3] == 'e') {
if (s[4] == 'i') {
if (features.cxxEnabled && s[4] == 'i') {
if (s[5] == 'd') {
return T_TYPEID;
}
@@ -614,7 +614,7 @@ static inline int classify7(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'm') {
else if (features.cxxEnabled && s[0] == 'm') {
if (s[1] == 'u') {
if (s[2] == 't') {
if (s[3] == 'a') {
@@ -644,7 +644,7 @@ static inline int classify7(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'p') {
else if (features.cxxEnabled && s[0] == 'p') {
if (s[1] == 'r') {
if (s[2] == 'i') {
if (s[3] == 'v') {
@@ -704,7 +704,7 @@ static inline int classify7(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'v') {
else if (features.cxxEnabled && s[0] == 'v') {
if (s[1] == 'i') {
if (s[2] == 'r') {
if (s[3] == 't') {
@@ -719,7 +719,7 @@ static inline int classify7(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'w') {
else if (features.cxxEnabled && s[0] == 'w') {
if (s[1] == 'c') {
if (s[2] == 'h') {
if (s[3] == 'a') {
@@ -873,7 +873,7 @@ static inline int classify8(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'e') {
else if (features.cxxEnabled && s[0] == 'e') {
if (s[1] == 'x') {
if (s[2] == 'p') {
if (s[3] == 'l') {
@@ -907,7 +907,7 @@ static inline int classify8(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'o') {
else if (features.cxxEnabled && s[0] == 'o') {
if (s[1] == 'p') {
if (s[2] == 'e') {
if (s[3] == 'r') {
@@ -941,7 +941,7 @@ static inline int classify8(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 't') {
else if (features.cxxEnabled && s[0] == 't') {
if (s[1] == 'e') {
if (s[2] == 'm') {
if (s[3] == 'p') {
@@ -1093,7 +1093,7 @@ static inline int classify9(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'n') {
else if (features.cxxEnabled && s[0] == 'n') {
if (s[1] == 'a') {
if (s[2] == 'm') {
if (s[3] == 'e') {
@@ -1112,7 +1112,7 @@ static inline int classify9(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'p') {
else if (features.cxxEnabled && s[0] == 'p') {
if (s[1] == 'r') {
if (s[2] == 'o') {
if (s[3] == 't') {
@@ -1188,7 +1188,7 @@ static inline int classify10(const char *s, LanguageFeatures features)
}
}
}
else if (s[2] == 'd') {
else if (features.cxxEnabled && s[2] == 'd') {
if (s[3] == 'e') {
if (s[4] == 'c') {
if (s[5] == 'l') {
@@ -1241,7 +1241,7 @@ static inline int classify10(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'c') {
else if (features.cxxEnabled && s[0] == 'c') {
if (s[1] == 'o') {
if (s[2] == 'n') {
if (s[3] == 's') {
@@ -1344,7 +1344,7 @@ static inline int classify11(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 's') {
else if (features.cxxEnabled && s[0] == 's') {
if (s[1] == 't') {
if (s[2] == 'a') {
if (s[3] == 't') {
@@ -1445,7 +1445,7 @@ static inline int classify12(const char *s, LanguageFeatures features)
}
}
}
else if (s[0] == 'd') {
else if (features.cxxEnabled && s[0] == 'd') {
if (s[1] == 'y') {
if (s[2] == 'n') {
if (s[3] == 'a') {
@@ -1556,9 +1556,9 @@ static inline int classify13(const char *s, LanguageFeatures features)
return T_IDENTIFIER;
}
static inline int classify16(const char *s, LanguageFeatures)
static inline int classify16(const char *s, LanguageFeatures features)
{
if (s[0] == 'r') {
if (features.cxxEnabled && s[0] == 'r') {
if (s[1] == 'e') {
if (s[2] == 'i') {
if (s[3] == 'n') {

View File

@@ -413,6 +413,7 @@ struct LanguageFeatures
unsigned int qtEnabled : 1; // If Qt is used.
unsigned int qtMocRunEnabled : 1;
unsigned int qtKeywordsEnabled : 1; // If Qt is used but QT_NO_KEYWORDS defined
unsigned int cxxEnabled : 1;
unsigned int cxx11Enabled : 1;
unsigned int objCEnabled : 1;
unsigned int c99Enabled : 1;

View File

@@ -201,6 +201,7 @@ static Tokens getTokens(const QTextCursor &cursor, int &prevState)
features.qtKeywordsEnabled = false;
features.qtMocRunEnabled = false;
features.cxx11Enabled = true;
features.cxxEnabled = true;
features.c99Enabled = true;
SimpleLexer tokenize;

View File

@@ -74,6 +74,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// FIXME: Check defaults or get from document.
LanguageFeatures features;
features.cxx11Enabled = true;
features.cxxEnabled = true;
features.c99Enabled = true;
SimpleLexer tokenize;

View File

@@ -1066,6 +1066,7 @@ int CodeFormatter::tokenizeBlock(const QTextBlock &block, bool *endedJoined)
features.qtEnabled = true;
features.qtMocRunEnabled = true;
features.qtKeywordsEnabled = true;
features.cxxEnabled = true;
features.objCEnabled = true;
SimpleLexer tokenize;

View File

@@ -45,6 +45,9 @@ void ProjectPart::updateLanguageFeatures()
{
const bool hasQt = qtVersion != NoQt;
languageFeatures.cxx11Enabled = languageVersion >= CXX11;
languageFeatures.cxxEnabled = languageVersion >= CXX98;
languageFeatures.c99Enabled = languageVersion >= C99;
languageFeatures.objCEnabled = languageExtensions & ObjectiveCExtensions;
languageFeatures.qtEnabled = hasQt;
languageFeatures.qtMocRunEnabled = hasQt;
if (!hasQt) {

View File

@@ -208,11 +208,11 @@ void ProjectPartBuilder::setConfigFileName(const QString &configFileName)
QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList &files,
FileClassifier fileClassifier)
{
QList<Core::Id> languages;
QSet<Core::Id> languages;
ProjectFileCategorizer cat(m_templatePart->displayName, files, fileClassifier);
if (cat.hasNoParts())
return languages;
return languages.toList();
using CppTools::ProjectFile;
using CppTools::ProjectPart;
@@ -223,7 +223,7 @@ QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList
ProjectPart::C11,
ProjectPart::NoExtensions);
// TODO: there is no C...
// languages += ProjectExplorer::Constants::LANG_C;
languages += ProjectExplorer::Constants::LANG_CXX;
}
if (cat.hasObjcSources()) {
createProjectPart(cat.objcSources(),
@@ -231,7 +231,7 @@ QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList
ProjectPart::C11,
ProjectPart::ObjectiveCExtensions);
// TODO: there is no Ojective-C...
// languages += ProjectExplorer::Constants::LANG_OBJC;
languages += ProjectExplorer::Constants::LANG_CXX;
}
if (cat.hasCxxSources()) {
createProjectPart(cat.cxxSources(),
@@ -249,7 +249,7 @@ QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList
languages += ProjectExplorer::Constants::LANG_CXX;
}
return languages;
return languages.toList();
}
namespace {
@@ -316,8 +316,6 @@ void ProjectPartBuilder::evaluateProjectPartToolchain(
languageVersion = ProjectPart::CXX11;
else if (flags & ToolChain::StandardCxx98)
languageVersion = ProjectPart::CXX98;
else
languageVersion = ProjectPart::CXX11;
auto &languageExtensions = projectPart->languageExtensions;
@@ -357,6 +355,7 @@ void ProjectPartBuilder::createProjectPart(const QVector<ProjectFile> &theSource
ProjectPart::Ptr part(m_templatePart->copy());
part->displayName = partName;
part->files = theSources;
part->languageVersion = languageVersion;
QTC_ASSERT(part->project, return);
if (ProjectExplorer::Target *activeTarget = part->project->activeTarget()) {

View File

@@ -51,6 +51,7 @@ public:
const StringLiteral *fileId = control.stringLiteral("<stdin>");
LanguageFeatures features;
features.cxx11Enabled = cxx11Enabled;
features.cxxEnabled = true;
features.objCEnabled = true;
features.qtEnabled = qtMocRun;
features.qtKeywordsEnabled = qtMocRun;

View File

@@ -141,6 +141,7 @@ class tst_cxx11: public QObject
if (file.open(QFile::ReadOnly)) {
LanguageFeatures features;
features.cxx11Enabled = true;
features.cxxEnabled = true;
features.c99Enabled = c99Enabled;
processDocument(doc, QTextStream(&file).readAll().toUtf8(), features, errors);
} else {
@@ -292,6 +293,7 @@ void tst_cxx11::lambdaType()
LanguageFeatures features;
features.cxx11Enabled = true;
features.cxxEnabled = true;
QByteArray errors;
Document::Ptr doc = Document::create(QLatin1String("testFile"));

View File

@@ -403,7 +403,9 @@ void tst_SimpleLexer::ppOpOrPunc()
QFETCH(Kind, expectedTokenKind);
const QByteArray source = QTest::currentDataTag();
run(source, toTokens({unsigned(expectedTokenKind)}), false, CompareKind, true);
LanguageFeatures languageFeatures;
languageFeatures.cxxEnabled = true;
run(source, toTokens({unsigned(expectedTokenKind)}), false, CompareKind, true, languageFeatures);
}
void tst_SimpleLexer::ppOpOrPunc_data()
@@ -574,6 +576,7 @@ void tst_SimpleLexer::user_defined_literals()
const TokenCompareFlags compareFlags = CompareKind | CompareBytes | CompareUtf16Chars | CompareUserDefinedLiteral;
LanguageFeatures languageFeatures;
languageFeatures.cxx11Enabled = true;
languageFeatures.cxxEnabled = true;
run(source, expectedTokens, false, compareFlags, false, languageFeatures);
}
@@ -626,7 +629,10 @@ void tst_SimpleLexer::offsets()
| CompareUtf16CharsBegin
| CompareUtf16CharsEnd
;
run(source, expectedTokens, false, compareFlags);
LanguageFeatures languageFeatures;
languageFeatures.cxxEnabled = true;
run(source, expectedTokens, false, compareFlags, false, languageFeatures);
}
void tst_SimpleLexer::offsets_data()

View File

@@ -142,6 +142,7 @@ public:
features.qtMocRunEnabled = qtMocRun;
features.qtKeywordsEnabled = qtMocRun;
features.cxx11Enabled = enableCxx11;
features.cxxEnabled = true;
diag.errorCount = 0; // reset the error count.
TranslationUnit *unit = parse(source, TranslationUnit::ParseTranlationUnit, features);
QSharedPointer<Document> doc(new Document(unit));