Index: include/clang/Driver/CC1Options.td =================================================================== --- include/clang/Driver/CC1Options.td +++ include/clang/Driver/CC1Options.td @@ -129,6 +129,9 @@ def analyzer_checker_help : Flag<["-"], "analyzer-checker-help">, HelpText<"Display the list of analyzer checkers that are available">; +def analyzer_list_configs : Flag<["-"], "analyzer-list-configs">, + HelpText<"Display the list of -analyzer-config options">; + def analyzer_list_enabled_checkers : Flag<["-"], "analyzer-list-enabled-checkers">, HelpText<"Display the list of enabled analyzer checkers">; Index: include/clang/StaticAnalyzer/Core/AnalyzerOptions.h =================================================================== --- include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -172,6 +172,7 @@ unsigned ShowCheckerHelp : 1; unsigned ShowEnabledCheckerList : 1; + unsigned ShowConfigOptionsList : 1; unsigned AnalyzeAll : 1; unsigned AnalyzerDisplayProgress : 1; unsigned AnalyzeNestedBlocks : 1; Index: include/clang/StaticAnalyzer/Frontend/FrontendActions.h =================================================================== --- include/clang/StaticAnalyzer/Frontend/FrontendActions.h +++ include/clang/StaticAnalyzer/Frontend/FrontendActions.h @@ -55,6 +55,7 @@ void printCheckerHelp(raw_ostream &OS, ArrayRef plugins); void printEnabledCheckerList(raw_ostream &OS, ArrayRef plugins, const AnalyzerOptions &opts); +void printAnalyzerConfigList(raw_ostream &OS); } // end GR namespace Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -279,6 +279,7 @@ } Opts.ShowCheckerHelp = Args.hasArg(OPT_analyzer_checker_help); + Opts.ShowConfigOptionsList = Args.hasArg(OPT_analyzer_list_configs); Opts.ShowEnabledCheckerList = Args.hasArg(OPT_analyzer_list_enabled_checkers); Opts.DisableAllChecks = Args.hasArg(OPT_analyzer_disable_all_checks); Index: lib/FrontendTool/ExecuteCompilerInvocation.cpp =================================================================== --- lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -241,11 +241,19 @@ ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins); return true; } + + // Honor -analyzer-list-enabled-checkers. if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) { ento::printEnabledCheckerList(llvm::outs(), Clang->getFrontendOpts().Plugins, *Clang->getAnalyzerOpts()); } + + // Honor -analyzer-list-enabled-checkers. + if (Clang->getAnalyzerOpts()->ShowConfigOptionsList) { + ento::printAnalyzerConfigList(llvm::outs()); + return true; + } #endif // If there were errors in processing arguments, don't do anything else. Index: lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp =================================================================== --- lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp +++ lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp @@ -157,3 +157,100 @@ SmallVector checkerOpts = getCheckerOptList(opts); ClangCheckerRegistry(plugins).printList(out, checkerOpts); } + +void ento::printAnalyzerConfigList(raw_ostream &out) { + out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n"; + out << "USAGE: clang [CLANG_OPTIONS] -analyzer-config " + "\n\n"; + out << " clang [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, " + "-analyzer-config OPTION2=VALUE, ...\n\n"; + out << "OPTIONS:\n\n"; + + using OptionAndDescriptionTy = std::pair; + OptionAndDescriptionTy PrintableOptions[] = { +#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC) \ + { CMDFLAG, DESC }, +#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" +#undef BOOL_ANALYZER_OPTION + }; + + llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS, + const OptionAndDescriptionTy &RHS) { + return LHS.first < RHS.first; + }); + + constexpr size_t PadForOptionName = 2; + constexpr size_t MaxWidth = 80; + constexpr size_t OptionNameMaxWidth = 30; + constexpr size_t MinDistance = 2; + + constexpr size_t DescriptionWidth = + MaxWidth - (PadForOptionName + OptionNameMaxWidth + MinDistance); + + constexpr size_t PadForDescription = + MaxWidth - DescriptionWidth + MinDistance; + + static_assert(MaxWidth == + PadForOptionName + OptionNameMaxWidth + MinDistance + DescriptionWidth, + "Wrong setup for printing -analyzer-config list!"); + + for (const auto &Pair : PrintableOptions) { + // Indent and print the option name. + out.indent(PadForOptionName) << Pair.first; + + // Indent before printing the description. + if (Pair.first.size() > OptionNameMaxWidth) { + // If the option's name is longer then OptionNameMaxWidth, print a + // newline. + out << '\n'; + out.indent(PadForDescription); + } else { + int CurrentLineWidth = PadForOptionName + Pair.first.size(); + out.indent(PadForDescription - CurrentLineWidth); + } + + // Print the description in a way that the entire list fits within MaxWidth + // columns. + + // Points to the first character in the description that wasn't printed + // just yet. + const char *NonPrintedTextStart = Pair.second.data(); + const char *const End = Pair.second.data() + Pair.second.size(); + while (NonPrintedTextStart < End) { + + // If the remainder of the description is shorter than DescriptionWidth. + if (NonPrintedTextStart + DescriptionWidth > End) { + out << std::string(NonPrintedTextStart, End); + break; + } + + // Look for a space around NonPrintedTextStart + DescriptionWidth. + + // First, look for the last space within + // [NonPrintedTextStart, NonPrintedTextStart + DescriptionWidth). + size_t LastPrintableCharPos = + StringRef(NonPrintedTextStart, DescriptionWidth).find_last_of(' '); + + // If we didn't find a space just yet, accept the fact that this line will + // not fit within MaxWidth columns, and look for the first space within + // [NonPrintedTextStart, std::end(NonPrintedTextStart)). + if (LastPrintableCharPos == StringRef::npos) + LastPrintableCharPos = + StringRef(NonPrintedTextStart).find_first_of(' '); + + // If there are no spaces left, print the remaining part of the + // description. + if (LastPrintableCharPos == StringRef::npos) { + out << std::string(NonPrintedTextStart, End); + break; + } + + out << StringRef(NonPrintedTextStart, LastPrintableCharPos) << '\n'; + out.indent(PadForDescription); + + // Step the pointer. + NonPrintedTextStart += LastPrintableCharPos + 1; + } + out << "\n\n"; + } +}