Skip to content

Commit 78957ba

Browse files
committed
[NFC] Refactor handling of Xarch option
Extract common code to a function. To prepare for adding an option for CUDA/HIP host and device only option. Differential Revision: https://reviews.llvm.org/D76455
1 parent 314deab commit 78957ba

File tree

5 files changed

+39
-76
lines changed

5 files changed

+39
-76
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ class ToolChain {
295295
const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost,
296296
SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const;
297297

298+
/// Append the argument following \p A to \p DAL assuming \p A is an Xarch
299+
/// argument.
300+
virtual void TranslateXarchArgs(const llvm::opt::DerivedArgList &Args,
301+
llvm::opt::Arg *&A,
302+
llvm::opt::DerivedArgList *DAL) const;
303+
298304
/// Choose a tool to use to handle the action \p JA.
299305
///
300306
/// This can be overridden when a particular ToolChain needs to use

clang/lib/Driver/ToolChain.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,3 +1102,33 @@ llvm::opt::DerivedArgList *ToolChain::TranslateOpenMPTargetArgs(
11021102
delete DAL;
11031103
return nullptr;
11041104
}
1105+
1106+
void ToolChain::TranslateXarchArgs(const llvm::opt::DerivedArgList &Args,
1107+
llvm::opt::Arg *&A,
1108+
llvm::opt::DerivedArgList *DAL) const {
1109+
const OptTable &Opts = getDriver().getOpts();
1110+
unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1111+
unsigned Prev = Index;
1112+
std::unique_ptr<llvm::opt::Arg> XarchArg(Opts.ParseOneArg(Args, Index));
1113+
1114+
// If the argument parsing failed or more than one argument was
1115+
// consumed, the -Xarch_ argument's parameter tried to consume
1116+
// extra arguments. Emit an error and ignore.
1117+
//
1118+
// We also want to disallow any options which would alter the
1119+
// driver behavior; that isn't going to work in our model. We
1120+
// use isDriverOption() as an approximation, although things
1121+
// like -O4 are going to slip through.
1122+
if (!XarchArg || Index > Prev + 1) {
1123+
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
1124+
<< A->getAsString(Args);
1125+
return;
1126+
} else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
1127+
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
1128+
<< A->getAsString(Args);
1129+
return;
1130+
}
1131+
XarchArg->setBaseArg(A);
1132+
A = XarchArg.release();
1133+
DAL->AddSynthesizedArg(A);
1134+
}

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -804,31 +804,7 @@ CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
804804
// Skip this argument unless the architecture matches BoundArch
805805
if (BoundArch.empty() || A->getValue(0) != BoundArch)
806806
continue;
807-
808-
unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
809-
unsigned Prev = Index;
810-
std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
811-
812-
// If the argument parsing failed or more than one argument was
813-
// consumed, the -Xarch_ argument's parameter tried to consume
814-
// extra arguments. Emit an error and ignore.
815-
//
816-
// We also want to disallow any options which would alter the
817-
// driver behavior; that isn't going to work in our model. We
818-
// use isDriverOption() as an approximation, although things
819-
// like -O4 are going to slip through.
820-
if (!XarchArg || Index > Prev + 1) {
821-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
822-
<< A->getAsString(Args);
823-
continue;
824-
} else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
825-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
826-
<< A->getAsString(Args);
827-
continue;
828-
}
829-
XarchArg->setBaseArg(A);
830-
A = XarchArg.release();
831-
DAL->AddSynthesizedArg(A);
807+
TranslateXarchArgs(Args, A, DAL);
832808
}
833809
DAL->append(A);
834810
}

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,32 +2132,7 @@ DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
21322132
continue;
21332133

21342134
Arg *OriginalArg = A;
2135-
unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
2136-
unsigned Prev = Index;
2137-
std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
2138-
2139-
// If the argument parsing failed or more than one argument was
2140-
// consumed, the -Xarch_ argument's parameter tried to consume
2141-
// extra arguments. Emit an error and ignore.
2142-
//
2143-
// We also want to disallow any options which would alter the
2144-
// driver behavior; that isn't going to work in our model. We
2145-
// use isDriverOption() as an approximation, although things
2146-
// like -O4 are going to slip through.
2147-
if (!XarchArg || Index > Prev + 1) {
2148-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
2149-
<< A->getAsString(Args);
2150-
continue;
2151-
} else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
2152-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
2153-
<< A->getAsString(Args);
2154-
continue;
2155-
}
2156-
2157-
XarchArg->setBaseArg(A);
2158-
2159-
A = XarchArg.release();
2160-
DAL->AddSynthesizedArg(A);
2135+
TranslateXarchArgs(Args, A, DAL);
21612136

21622137
// Linker input arguments require custom handling. The problem is that we
21632138
// have already constructed the phase actions, so we can not treat them as

clang/lib/Driver/ToolChains/HIP.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -382,31 +382,7 @@ HIPToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
382382
// Skip this argument unless the architecture matches BoundArch.
383383
if (BoundArch.empty() || A->getValue(0) != BoundArch)
384384
continue;
385-
386-
unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
387-
unsigned Prev = Index;
388-
std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
389-
390-
// If the argument parsing failed or more than one argument was
391-
// consumed, the -Xarch_ argument's parameter tried to consume
392-
// extra arguments. Emit an error and ignore.
393-
//
394-
// We also want to disallow any options which would alter the
395-
// driver behavior; that isn't going to work in our model. We
396-
// use isDriverOption() as an approximation, although things
397-
// like -O4 are going to slip through.
398-
if (!XarchArg || Index > Prev + 1) {
399-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
400-
<< A->getAsString(Args);
401-
continue;
402-
} else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
403-
getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
404-
<< A->getAsString(Args);
405-
continue;
406-
}
407-
XarchArg->setBaseArg(A);
408-
A = XarchArg.release();
409-
DAL->AddSynthesizedArg(A);
385+
TranslateXarchArgs(Args, A, DAL);
410386
}
411387
DAL->append(A);
412388
}

0 commit comments

Comments
 (0)