Skip to content

Commit c70b0e8

Browse files
committed
[clang-cl] Add /permissive and /permissive-
This patch adds the command line options /permissive and /permissive- to clang-cl. These flags are used in MSVC to enable various /Zc language conformance options at once. In particular, /permissive is used to enable the various non standard behaviour of MSVC, while /permissive- is the opposite. When either of two command lines are specified they are simply expanded to the various underlying /Zc options. In particular when /permissive is passed it currently expands to: /Zc:twoPhase- (disable two phase lookup) -fno-operator-names (disable C++ operator keywords) /permissive- expands to the opposites of these flags + /Zc:strictStrings (/Zc:strictStrings- does not currently exist). In the future, if any more MSVC workarounds are ever added they can easily be added to the expansion. One is also able to override settings done by permissive. Specifying /permissive- /Zc:twoPhase- will apply the settings from permissive minus, but disables two phase lookup. Motivation for this patch was mainly parity with MSVC as well as compatibility with Windows SDK headers. The /permissive page from MSVC documents various workarounds that have to be done for the Windows SDK headers [1], when MSVC is used with /permissive-. In these, Microsoft often recommends simply compiling with /permissive for the specified source files. Since some of these also apply to clang-cl (which acts like /permissive- by default mostly), and some are currently implemented as "hacks" within clang that I'd like to remove, adding /permissive and /permissive- to be in full parity with MSVC and Microsofts documentation made sense to me. [1] https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-160#windows-header-issues Differential Revision: https://reviews.llvm.org/D103773
1 parent 936d675 commit c70b0e8

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6099,6 +6099,10 @@ def _SLASH_o : CLJoinedOrSeparate<"o">,
60996099
HelpText<"Deprecated (set output file name); use /Fe or /Fe">,
61006100
MetaVarName<"<file or dir/>">;
61016101
def _SLASH_P : CLFlag<"P">, HelpText<"Preprocess to file">;
6102+
def _SLASH_permissive : CLFlag<"permissive">,
6103+
HelpText<"Enable some non conforming code to compile">;
6104+
def _SLASH_permissive_ : CLFlag<"permissive-">,
6105+
HelpText<"Disable non conforming code from compiling (default)">;
61026106
def _SLASH_Tc : CLCompileJoinedOrSeparate<"Tc">,
61036107
HelpText<"Treat <file> as C source file">, MetaVarName<"<file>">;
61046108
def _SLASH_TC : CLCompileFlag<"TC">, HelpText<"Treat all source files as C">;
@@ -6180,7 +6184,6 @@ def _SLASH_FS : CLIgnoredFlag<"FS">;
61806184
def _SLASH_JMC : CLIgnoredFlag<"JMC">;
61816185
def _SLASH_kernel_ : CLIgnoredFlag<"kernel-">;
61826186
def _SLASH_nologo : CLIgnoredFlag<"nologo">;
6183-
def _SLASH_permissive_ : CLIgnoredFlag<"permissive-">;
61846187
def _SLASH_RTC : CLIgnoredJoined<"RTC">;
61856188
def _SLASH_sdl : CLIgnoredFlag<"sdl">;
61866189
def _SLASH_sdl_ : CLIgnoredFlag<"sdl-">;

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,20 @@ static void TranslateDArg(Arg *A, llvm::opt::DerivedArgList &DAL,
15231523
DAL.AddJoinedArg(A, Opts.getOption(options::OPT_D), NewVal);
15241524
}
15251525

1526+
static void TranslatePermissive(Arg *A, llvm::opt::DerivedArgList &DAL,
1527+
const OptTable &Opts) {
1528+
DAL.AddFlagArg(A, Opts.getOption(options::OPT__SLASH_Zc_twoPhase_));
1529+
DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_operator_names));
1530+
// There is currently no /Zc:strictStrings- in clang-cl
1531+
}
1532+
1533+
static void TranslatePermissiveMinus(Arg *A, llvm::opt::DerivedArgList &DAL,
1534+
const OptTable &Opts) {
1535+
DAL.AddFlagArg(A, Opts.getOption(options::OPT__SLASH_Zc_twoPhase));
1536+
DAL.AddFlagArg(A, Opts.getOption(options::OPT_foperator_names));
1537+
DAL.AddFlagArg(A, Opts.getOption(options::OPT__SLASH_Zc_strictStrings));
1538+
}
1539+
15261540
llvm::opt::DerivedArgList *
15271541
MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
15281542
StringRef BoundArch,
@@ -1565,6 +1579,12 @@ MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
15651579
} else if (A->getOption().matches(options::OPT_D)) {
15661580
// Translate -Dfoo#bar into -Dfoo=bar.
15671581
TranslateDArg(A, *DAL, Opts);
1582+
} else if (A->getOption().matches(options::OPT__SLASH_permissive)) {
1583+
// Expand /permissive
1584+
TranslatePermissive(A, *DAL, Opts);
1585+
} else if (A->getOption().matches(options::OPT__SLASH_permissive_)) {
1586+
// Expand /permissive-
1587+
TranslatePermissiveMinus(A, *DAL, Opts);
15681588
} else if (OFK != Action::OFK_HIP) {
15691589
// HIP Toolchain translates input args by itself.
15701590
DAL->append(A);

clang/test/Driver/cl-permissive.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Note: %s must be preceded by --, otherwise it may be interpreted as a
2+
// command-line option, e.g. on Mac where %s is commonly under /Users.
3+
4+
// RUN: %clang_cl /permissive -### -- %s 2>&1 | FileCheck -check-prefix=PERMISSIVE %s
5+
// PERMISSIVE: "-fno-operator-names"
6+
// PERMISSIVE: "-fdelayed-template-parsing"
7+
// RUN: %clang_cl /permissive- -### -- %s 2>&1 | FileCheck -check-prefix=PERMISSIVE-MINUS %s
8+
// PERMISSIVE-MINUS-NOT: "-fno-operator-names"
9+
// PERMISSIVE-MINUS-NOT: "-fdelayed-template-parsing"
10+
11+
// The switches set by permissive may then still be manually enabled or disabled
12+
// RUN: %clang_cl /permissive /Zc:twoPhase -### -- %s 2>&1 | FileCheck -check-prefix=PERMISSIVE-OVERWRITE %s
13+
// PERMISSIVE-OVERWRITE: "-fno-operator-names"
14+
// PERMISSIVE-OVERWRITE-NOT: "-fdelayed-template-parsing"
15+
// RUN: %clang_cl /permissive- /Zc:twoPhase- -### -- %s 2>&1 | FileCheck -check-prefix=PERMISSIVE-MINUS-OVERWRITE %s
16+
// PERMISSIVE-MINUS-OVERWRITE-NOT: "-fno-operator-names"
17+
// PERMISSIVE-MINUS-OVERWRITE: "-fdelayed-template-parsing"

0 commit comments

Comments
 (0)