rnk created this revision.
rnk added reviewers: zero9178, mibintc, erichkeane.
rnk requested review of this revision.
Herald added a project: clang.
This change would treat the token `or` in system headers as an
identifier, and elsewhere as an operator. As reported in
llvm.org/pr42427, many users classify their third party library headers
as "system" headers to suppress warnings. There's no clean way to
separate Windows SDK headers from user headers.
Since D103773 <https://reviews.llvm.org/D103773>, clang-cl's default mode
completely disables C++ operator
names. That means that query.h compiles out of the box without this hack
in the pre-processor. For users who are using /permissive-, they must
set the QUERY_H_RESTRICTION_PERMISSIVE macro to rename this problematic
`or` field.
By reverting this change, we get to a consistent state: C++ operator
names are either enabled or disabled globally. If that breaks
compilation because legacy code is using `or` as an identifier, the user
has flags to work around the problem (`/permissive` or
`/clang:-fno-operator-names`).
Fixes PR42427
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D108720
Files:
clang/lib/Lex/Preprocessor.cpp
clang/test/Headers/ms-cppoperkey.cpp
clang/test/Headers/ms-cppoperkey1.cpp
clang/test/Headers/ms-cppoperkey2.cpp
Index: clang/test/Headers/ms-cppoperkey2.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey2.cpp
+++ /dev/null
@@ -1,11 +0,0 @@
-// RUN: %clang_cc1 -triple x86_64-pc-win32 -fms-compatibility \
-// RUN: -ffreestanding -fsyntax-only -Werror %s -verify
-// RUN: %clang_cc1 \
-// RUN: -ffreestanding -fsyntax-only -Werror %s -verify
-// expected-no-diagnostics
-int bb ( int x)
-{
- // In user code, treat operator keyword as operator keyword.
- if ( x>1 or x<0) return 1;
- else return 0;
-}
Index: clang/test/Headers/ms-cppoperkey1.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey1.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-// RUN: %clang_cc1 \
-// RUN: -fms-compatibility -x c++-cpp-output \
-// RUN: -ffreestanding -fsyntax-only -Werror \
-// RUN: %s -verify
-
-
-# 1 "t.cpp"
-# 1 "query.h" 1 3 4
-// MS header <query.h> uses operator keyword as field name.
-// Compile without syntax errors.
-struct tagRESTRICTION
- {
- union _URes
- {
- int or; // Note use of cpp operator token
- } res;
- };
- ;
-
-int aa ( int x)
-{
- // In system header code, treat operator keyword as identifier.
- if ( // expected-note{{to match this '('}}
- x>1 or x<0) return 1; // expected-error{{expected ')'}}
- else return 0;
-}
-
Index: clang/test/Headers/ms-cppoperkey.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-// RUN: %clang_cc1 \
-// RUN: -fms-compatibility -x c++-cpp-output \
-// RUN: -ffreestanding -fsyntax-only -Werror \
-// RUN: %s -verify
-// expected-no-diagnostics
-# 1 "t.cpp"
-# 1 "query.h" 1 3
-// MS header <query.h> uses operator keyword as field name.
-// Compile without syntax errors.
-struct tagRESTRICTION
- {
- union _URes
- {
- int or; // Note use of cpp operator token
- } res;
- };
Index: clang/lib/Lex/Preprocessor.cpp
===================================================================
--- clang/lib/Lex/Preprocessor.cpp
+++ clang/lib/Lex/Preprocessor.cpp
@@ -723,11 +723,7 @@
// is cleaned to tok::identifier "B". After cleaning the token's length is
// still 3 and the SourceLocation refers to the location of the backslash.
Identifier.setIdentifierInfo(II);
- if (getLangOpts().MSVCCompat && II->isCPlusPlusOperatorKeyword() &&
- getSourceManager().isInSystemHeader(Identifier.getLocation()))
- Identifier.setKind(tok::identifier);
- else
- Identifier.setKind(II->getTokenID());
+ Identifier.setKind(II->getTokenID());
return II;
}
Index: clang/test/Headers/ms-cppoperkey2.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey2.cpp
+++ /dev/null
@@ -1,11 +0,0 @@
-// RUN: %clang_cc1 -triple x86_64-pc-win32 -fms-compatibility \
-// RUN: -ffreestanding -fsyntax-only -Werror %s -verify
-// RUN: %clang_cc1 \
-// RUN: -ffreestanding -fsyntax-only -Werror %s -verify
-// expected-no-diagnostics
-int bb ( int x)
-{
- // In user code, treat operator keyword as operator keyword.
- if ( x>1 or x<0) return 1;
- else return 0;
-}
Index: clang/test/Headers/ms-cppoperkey1.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey1.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-// RUN: %clang_cc1 \
-// RUN: -fms-compatibility -x c++-cpp-output \
-// RUN: -ffreestanding -fsyntax-only -Werror \
-// RUN: %s -verify
-
-
-# 1 "t.cpp"
-# 1 "query.h" 1 3 4
-// MS header <query.h> uses operator keyword as field name.
-// Compile without syntax errors.
-struct tagRESTRICTION
- {
- union _URes
- {
- int or; // Note use of cpp operator token
- } res;
- };
- ;
-
-int aa ( int x)
-{
- // In system header code, treat operator keyword as identifier.
- if ( // expected-note{{to match this '('}}
- x>1 or x<0) return 1; // expected-error{{expected ')'}}
- else return 0;
-}
-
Index: clang/test/Headers/ms-cppoperkey.cpp
===================================================================
--- clang/test/Headers/ms-cppoperkey.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-// RUN: %clang_cc1 \
-// RUN: -fms-compatibility -x c++-cpp-output \
-// RUN: -ffreestanding -fsyntax-only -Werror \
-// RUN: %s -verify
-// expected-no-diagnostics
-# 1 "t.cpp"
-# 1 "query.h" 1 3
-// MS header <query.h> uses operator keyword as field name.
-// Compile without syntax errors.
-struct tagRESTRICTION
- {
- union _URes
- {
- int or; // Note use of cpp operator token
- } res;
- };
Index: clang/lib/Lex/Preprocessor.cpp
===================================================================
--- clang/lib/Lex/Preprocessor.cpp
+++ clang/lib/Lex/Preprocessor.cpp
@@ -723,11 +723,7 @@
// is cleaned to tok::identifier "B". After cleaning the token's length is
// still 3 and the SourceLocation refers to the location of the backslash.
Identifier.setIdentifierInfo(II);
- if (getLangOpts().MSVCCompat && II->isCPlusPlusOperatorKeyword() &&
- getSourceManager().isInSystemHeader(Identifier.getLocation()))
- Identifier.setKind(tok::identifier);
- else
- Identifier.setKind(II->getTokenID());
+ Identifier.setKind(II->getTokenID());
return II;
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits