Skip to content

Commit a828fb4

Browse files
committed
[clang][cli] Port a CommaJoined option to the marshalling infrastructure
Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D93698
1 parent 801c786 commit a828fb4

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ def fansi_escape_codes : Flag<["-"], "fansi-escape-codes">, Group<f_Group>,
12011201
MarshallingInfoFlag<"DiagnosticOpts->UseANSIEscapeCodes">;
12021202
def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Group<f_clang_Group>, Flags<[CC1Option]>,
12031203
HelpText<"Treat each comma separated argument in <arg> as a documentation comment block command">,
1204-
MetaVarName<"<arg>">;
1204+
MetaVarName<"<arg>">, MarshallingInfoStringVector<"LangOpts->CommentOpts.BlockCommandNames">;
12051205
def fparse_all_comments : Flag<["-"], "fparse-all-comments">, Group<f_clang_Group>, Flags<[CC1Option]>,
12061206
MarshallingInfoFlag<"LangOpts->CommentOpts.ParseAllComments">;
12071207
def frecord_command_line : Flag<["-"], "frecord-command-line">,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,20 @@ static void denormalizeStringVector(SmallVectorImpl<const char *> &Args,
328328
Option::OptionClass OptClass,
329329
unsigned TableIndex,
330330
const std::vector<std::string> &Values) {
331-
for (const std::string &Value : Values) {
332-
denormalizeString(Args, Spelling, SA, OptClass, TableIndex, Value);
331+
if (OptClass == Option::OptionClass::CommaJoinedClass) {
332+
std::string CommaJoinedValue;
333+
if (!Values.empty()) {
334+
CommaJoinedValue.append(Values.front());
335+
for (const std::string &Value : llvm::drop_begin(Values, 1)) {
336+
CommaJoinedValue.append(",");
337+
CommaJoinedValue.append(Value);
338+
}
339+
}
340+
denormalizeString(Args, Spelling, SA, Option::OptionClass::JoinedClass,
341+
TableIndex, CommaJoinedValue);
342+
} else if (OptClass == Option::OptionClass::JoinedClass) {
343+
for (const std::string &Value : Values)
344+
denormalizeString(Args, Spelling, SA, OptClass, TableIndex, Value);
333345
}
334346
}
335347

@@ -785,7 +797,6 @@ static void parseAnalyzerConfigs(AnalyzerOptions &AnOpts,
785797
}
786798

787799
static void ParseCommentArgs(CommentOptions &Opts, ArgList &Args) {
788-
Opts.BlockCommandNames = Args.getAllArgValues(OPT_fcomment_block_commands);
789800
Opts.ParseAllComments = Args.hasArg(OPT_fparse_all_comments);
790801
}
791802

clang/unittests/Frontend/CompilerInvocationTest.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,53 @@ TEST_F(CommandLineTest, StringVectorMultiple) {
508508
ASSERT_THAT(GeneratedArgs, ContainsN(HasSubstr("-fmodule-map-file"), 2));
509509
}
510510

511+
// CommaJoined option with MarshallingInfoStringVector.
512+
513+
TEST_F(CommandLineTest, StringVectorCommaJoinedNone) {
514+
const char *Args[] = {""};
515+
516+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
517+
518+
ASSERT_FALSE(Diags->hasErrorOccurred());
519+
ASSERT_TRUE(Invocation.getLangOpts()->CommentOpts.BlockCommandNames.empty());
520+
521+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
522+
523+
ASSERT_THAT(GeneratedArgs,
524+
Not(Contains(HasSubstr("-fcomment-block-commands"))));
525+
}
526+
527+
TEST_F(CommandLineTest, StringVectorCommaJoinedSingle) {
528+
const char *Args[] = {"-fcomment-block-commands=x,y"};
529+
530+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
531+
532+
ASSERT_FALSE(Diags->hasErrorOccurred());
533+
ASSERT_EQ(Invocation.getLangOpts()->CommentOpts.BlockCommandNames,
534+
std::vector<std::string>({"x", "y"}));
535+
536+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
537+
538+
ASSERT_THAT(GeneratedArgs,
539+
ContainsN(StrEq("-fcomment-block-commands=x,y"), 1));
540+
}
541+
542+
TEST_F(CommandLineTest, StringVectorCommaJoinedMultiple) {
543+
const char *Args[] = {"-fcomment-block-commands=x,y",
544+
"-fcomment-block-commands=a,b"};
545+
546+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
547+
548+
ASSERT_FALSE(Diags->hasErrorOccurred());
549+
ASSERT_EQ(Invocation.getLangOpts()->CommentOpts.BlockCommandNames,
550+
std::vector<std::string>({"x", "y", "a", "b"}));
551+
552+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
553+
554+
ASSERT_THAT(GeneratedArgs,
555+
ContainsN(StrEq("-fcomment-block-commands=x,y,a,b"), 1));
556+
}
557+
511558
// A flag that should be parsed only if a condition is met.
512559

513560
TEST_F(CommandLineTest, ConditionalParsingIfFalseFlagNotPresent) {

0 commit comments

Comments
 (0)