Skip to content

Commit b125329

Browse files
committed
add support for summary format selection
1 parent 03fc645 commit b125329

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5954,6 +5954,9 @@ def emit_summaries : Flag<["-", "--"], "emit-summaries">, Flags<[NoXarchOption]>
59545954
Visibility<[ClangOption]>,
59555955
Alias<emit_summaries_EQ>, AliasArgs<["cwd"]>,
59565956
HelpText<"Alias for --emit-summaries=cwd">;
5957+
def summary_format_EQ : Joined<["-", "--"], "summary-format=">, Flags<[NoXarchOption]>,
5958+
Visibility<[ClangOption, CC1Option]>,
5959+
HelpText<"The format of the emitted summaries. Can be set to 'json' (default) or 'yaml'">;
59575960
def save_stats_EQ : Joined<["-", "--"], "save-stats=">, Flags<[NoXarchOption]>,
59585961
HelpText<"Save llvm statistics.">;
59595962
def save_stats : Flag<["-", "--"], "save-stats">, Flags<[NoXarchOption]>,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ class FrontendOptions {
540540
/// The directory used to load summary files.
541541
std::string SummaryDirPath;
542542

543+
/// The format of the emitted summary files.
544+
std::string SummaryFormat;
545+
543546
public:
544547
FrontendOptions()
545548
: DisableFree(false), RelocatablePCH(false), ShowHelp(false),

clang/include/clang/Summary/SummarySerialization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class JSONSummarySerializer : public SummarySerializer {
3030
};
3131

3232
class YAMLSummarySerializer : public SummarySerializer {
33+
public:
3334
YAMLSummarySerializer(SummaryContext &SummaryCtx)
3435
: SummarySerializer(SummaryCtx){};
3536

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5473,6 +5473,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54735473
if (Args.getLastArg(options::OPT_summaries_dir_EQ))
54745474
Args.AddLastArg(CmdArgs, options::OPT_summaries_dir_EQ);
54755475

5476+
std::string SummaryFormat = "json";
5477+
if (Arg *A = Args.getLastArg(options::OPT_summary_format_EQ)) {
5478+
// FIXME: This logic is duplicated, so something is clearly wrong here...
5479+
StringRef Format = A->getValue();
5480+
if (Format == "json" || Format == "yaml")
5481+
SummaryFormat = Format;
5482+
5483+
Args.AddLastArg(CmdArgs, options::OPT_summary_format_EQ);
5484+
}
5485+
5486+
// FIXME: This arg shouldn't exist...
54765487
if (const Arg *A = Args.getLastArg(options::OPT_emit_summaries_EQ)) {
54775488
llvm::SmallString<10> input;
54785489
for (const auto &II : Inputs) {
@@ -5496,7 +5507,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54965507
}
54975508

54985509
if (!summaryFile.empty()) {
5499-
llvm::sys::path::replace_extension(summaryFile, "json");
5510+
llvm::sys::path::replace_extension(summaryFile, SummaryFormat);
55005511
CmdArgs.push_back(
55015512
Args.MakeArgString(Twine("-summary-file=") + summaryFile));
55025513
}

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,15 @@ void CompilerInstance::createSummaryConsumer() {
765765
}
766766

767767
void CompilerInstance::createSummarySerializer() {
768-
TheSummarySerializer.reset(new JSONSummarySerializer(getSummaryContext()));
768+
StringRef Format = getFrontendOpts().SummaryFormat;
769+
SummarySerializer *Serializer;
770+
771+
if (Format == "yaml")
772+
Serializer = new YAMLSummarySerializer(getSummaryContext());
773+
else
774+
Serializer = new JSONSummarySerializer(getSummaryContext());
775+
776+
TheSummarySerializer.reset(Serializer);
769777
}
770778

771779
void CompilerInstance::createSema(TranslationUnitKind TUKind,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,8 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts,
29852985
Lang + HeaderUnit + Header + ModuleMap + Preprocessed);
29862986
}
29872987

2988+
GenerateArg(Consumer, OPT_summary_format_EQ, Opts.SummaryFormat);
2989+
29882990
// OPT_INPUT has a unique class, generate it directly.
29892991
for (const auto &Input : Opts.Inputs)
29902992
Consumer(Input.getFile());
@@ -3263,6 +3265,15 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
32633265
Opts.Inputs.emplace_back(std::move(Inputs[i]), IK, IsSystem);
32643266
}
32653267

3268+
Opts.SummaryFormat = "json";
3269+
if (const Arg *A = Args.getLastArg(OPT_summary_format_EQ)) {
3270+
StringRef Format = A->getValue();
3271+
3272+
// FIXME: don't hardcode these values
3273+
if (Format == "json" || Format == "yaml")
3274+
Opts.SummaryFormat = Format;
3275+
};
3276+
32663277
Opts.DashX = DashX;
32673278

32683279
return Diags.getNumErrors() == NumErrorsBefore;

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -978,18 +978,21 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
978978
if (!CI.getFrontendOpts().SummaryDirPath.empty()) {
979979
// FIXME: this is a quick shortcut so large summaries are only evaluated
980980
// once, we should think about implementing it in a reasonable way...
981-
static const char *reducedCache =
982-
"reduced-summary-so-that-we-do-not-have-to-evaluate-it-every-time.json";
983-
FileManager &FileMgr = CI.getFileManager();
981+
static const char *reducedCacheName =
982+
"reduced-summary-so-that-we-do-not-have-to-evaluate-it-every-time";
983+
const std::string summaryExtension =
984+
'.' + CI.getFrontendOpts().SummaryFormat;
984985

986+
FileManager &FileMgr = CI.getFileManager();
985987
StringRef SummaryDirPath = CI.getFrontendOpts().SummaryDirPath;
986988
if (auto SummaryDir = FileMgr.getOptionalDirectoryRef(SummaryDirPath)) {
987989
std::error_code EC;
988990
SmallString<128> DirNative;
989991
llvm::sys::path::native(SummaryDir->getName(), DirNative);
990992

991993
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
992-
std::string cacheFile = DirNative.str().str() + '/' + reducedCache;
994+
std::string cacheFile =
995+
DirNative.str().str() + '/' + reducedCacheName + summaryExtension;
993996

994997
std::vector<std::string> paths;
995998

@@ -999,7 +1002,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
9991002
for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
10001003
DirEnd;
10011004
Dir != DirEnd && !EC; Dir.increment(EC)) {
1002-
if (llvm::sys::path::extension(Dir->path()) != ".json")
1005+
if (llvm::sys::path::extension(Dir->path()) != summaryExtension)
10031006
continue;
10041007

10051008
paths.emplace_back(Dir->path().str());

0 commit comments

Comments
 (0)