Introduce a limit on the depth of the macro instantiation backtrace

printed in a diagnostic, similar to the limit we already have on the
depth of the template instantiation backtrace. The macro instantiation
backtrace is limited to 10 "instantiated from:" diagnostics; when it's
longer than that, we'll show the first half, then say how many were
suppressed, then show the second half. The limit can be changed with
-fmacro-instantiation-limit=N, and turned off with N=0.

This eliminates a lot of note spew with libraries making use of the
Boost.Preprocess library.

llvm-svn: 103014
This commit is contained in:
Douglas Gregor 2010-05-04 17:13:42 +00:00
parent a5c0cc329e
commit cd121fb013
10 changed files with 134 additions and 30 deletions

View File

@ -283,13 +283,13 @@ public:
void setTemplateBacktraceLimit(unsigned Limit) {
TemplateBacktraceLimit = Limit;
}
/// \brief Retrieve the maximum number of template instantiation
/// nodes to emit along with a given diagnostic.
unsigned getTemplateBacktraceLimit() const {
return TemplateBacktraceLimit;
}
/// setIgnoreAllWarnings - When set to true, any unmapped warnings are
/// ignored. If this and WarningsAsErrors are both set, then this one wins.
void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }

View File

@ -1457,7 +1457,7 @@ def note_template_default_arg_checking : Note<
"while checking a default template argument used here">;
def note_instantiation_contexts_suppressed : Note<
"(skipping %0 context%s0 in backtrace; use -ftemplate-backtrace-limit=0 to "
"see all)">;
"see all)">;
def err_field_instantiates_to_function : Error<
"data member instantiated with function type %0">;

View File

@ -200,6 +200,8 @@ def ftabstop : Separate<"-ftabstop">, MetaVarName<"<N>">,
HelpText<"Set the tab stop distance.">;
def ferror_limit : Separate<"-ferror-limit">, MetaVarName<"<N>">,
HelpText<"Set the maximum number of errors to emit before stopping (0 = no limit).">;
def fmacro_backtrace_limit : Separate<"-fmacro-backtrace-limit">, MetaVarName<"<N>">,
HelpText<"Set the maximum number of entries to print in a macro instantiation backtrace (0 = no limit).">;
def ftemplate_backtrace_limit : Separate<"-ftemplate-backtrace-limit">, MetaVarName<"<N>">,
HelpText<"Set the maximum number of entries to print in a template instantiation backtrace (0 = no limit).">;
def fmessage_length : Separate<"-fmessage-length">, MetaVarName<"<N>">,

View File

@ -280,6 +280,8 @@ def flat__namespace : Flag<"-flat_namespace">;
def flax_vector_conversions : Flag<"-flax-vector-conversions">, Group<f_Group>;
def flimited_precision_EQ : Joined<"-flimited-precision=">, Group<f_Group>;
def flto : Flag<"-flto">, Group<f_Group>;
def fmacro_backtrace_limit_EQ : Joined<"-fmacro-backtrace-limit=">,
Group<f_Group>;
def fmath_errno : Flag<"-fmath-errno">, Group<f_Group>;
def fmerge_all_constants : Flag<"-fmerge-all-constants">, Group<f_Group>;
def fmessage_length_EQ : Joined<"-fmessage-length=">, Group<f_Group>;

View File

@ -39,11 +39,15 @@ public:
/// deserialized by, e.g., the CIndex library.
unsigned ErrorLimit; /// Limit # errors emitted.
unsigned MacroBacktraceLimit; /// Limit depth of macro instantiation
/// backtrace.
unsigned TemplateBacktraceLimit; /// Limit depth of instantiation backtrace.
/// The distance between tab stops.
unsigned TabStop;
enum { DefaultTabStop = 8, MaxTabStop = 100 };
enum { DefaultTabStop = 8, MaxTabStop = 100,
DefaultMacroBacktraceLimit = 6,
DefaultTemplateBacktraceLimit = 10 };
/// Column limit for formatting message diagnostics, or 0 if unused.
unsigned MessageLength;
@ -75,6 +79,7 @@ public:
BinaryOutput = 0;
ErrorLimit = 0;
TemplateBacktraceLimit = 0;
MacroBacktraceLimit = 0;
}
};

View File

@ -71,12 +71,15 @@ public:
const SourceManager &SM,
const FixItHint *Hints,
unsigned NumHints,
unsigned Columns);
unsigned Columns,
unsigned OnMacroInst,
unsigned MacroSkipStart,
unsigned MacroSkipEnd);
virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
const DiagnosticInfo &Info);
};
} // end namspace clang
} // end namespace clang
#endif

View File

@ -21,6 +21,7 @@
#include "clang/Driver/Options.h"
#include "clang/Driver/ToolChain.h"
#include "clang/Driver/Util.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSwitch.h"
@ -1082,11 +1083,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
else
CmdArgs.push_back("19");
CmdArgs.push_back("-fmacro-backtrace-limit");
if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ))
CmdArgs.push_back(A->getValue(Args));
else
CmdArgs.push_back(Args.MakeArgString(
llvm::Twine(DiagnosticOptions::DefaultMacroBacktraceLimit)));
CmdArgs.push_back("-ftemplate-backtrace-limit");
if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ))
CmdArgs.push_back(A->getValue(Args));
else
CmdArgs.push_back("10");
CmdArgs.push_back(Args.MakeArgString(
llvm::Twine(DiagnosticOptions::DefaultTemplateBacktraceLimit)));
// Pass -fmessage-length=.
CmdArgs.push_back("-fmessage-length");

View File

@ -247,7 +247,13 @@ static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
Res.push_back("-ferror-limit");
Res.push_back(llvm::utostr(Opts.ErrorLimit));
}
if (Opts.TemplateBacktraceLimit != 10) {
if (Opts.MacroBacktraceLimit
!= DiagnosticOptions::DefaultMacroBacktraceLimit) {
Res.push_back("-fmacro-backtrace-limit");
Res.push_back(llvm::utostr(Opts.MacroBacktraceLimit));
}
if (Opts.TemplateBacktraceLimit
!= DiagnosticOptions::DefaultTemplateBacktraceLimit) {
Res.push_back("-ftemplate-backtrace-limit");
Res.push_back(llvm::utostr(Opts.TemplateBacktraceLimit));
}
@ -877,8 +883,13 @@ static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
Opts.VerifyDiagnostics = Args.hasArg(OPT_verify);
Opts.BinaryOutput = Args.hasArg(OPT_fdiagnostics_binary);
Opts.ErrorLimit = getLastArgIntValue(Args, OPT_ferror_limit, 0, Diags);
Opts.MacroBacktraceLimit
= getLastArgIntValue(Args, OPT_fmacro_backtrace_limit,
DiagnosticOptions::DefaultMacroBacktraceLimit, Diags);
Opts.TemplateBacktraceLimit
= getLastArgIntValue(Args, OPT_ftemplate_backtrace_limit, 0, Diags);
= getLastArgIntValue(Args, OPT_ftemplate_backtrace_limit,
DiagnosticOptions::DefaultTemplateBacktraceLimit,
Diags);
Opts.TabStop = getLastArgIntValue(Args, OPT_ftabstop,
DiagnosticOptions::DefaultTabStop, Diags);
if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop) {

View File

@ -285,7 +285,10 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
const SourceManager &SM,
const FixItHint *Hints,
unsigned NumHints,
unsigned Columns) {
unsigned Columns,
unsigned OnMacroInst,
unsigned MacroSkipStart,
unsigned MacroSkipEnd) {
assert(LangOpts && "Unexpected diagnostic outside source file processing");
assert(!Loc.isInvalid() && "must have a valid source location here");
@ -293,10 +296,16 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
// instantiated (recursively) then emit information about where the token was
// spelled from.
if (!Loc.isFileID()) {
// Whether to suppress printing this macro instantiation.
bool Suppressed
= OnMacroInst >= MacroSkipStart && OnMacroInst < MacroSkipEnd;
SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
// FIXME: Map ranges?
EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns);
EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns,
OnMacroInst + 1, MacroSkipStart, MacroSkipEnd);
// Map the location.
Loc = SM.getImmediateSpellingLoc(Loc);
@ -308,26 +317,38 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Ranges[i] = SourceRange(S, E);
}
// Get the pretty name, according to #line directives etc.
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
if (!Suppressed) {
// Get the pretty name, according to #line directives etc.
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
// If this diagnostic is not in the main file, print out the "included from"
// lines.
if (LastWarningLoc != PLoc.getIncludeLoc()) {
LastWarningLoc = PLoc.getIncludeLoc();
PrintIncludeStack(LastWarningLoc, SM);
// If this diagnostic is not in the main file, print out the
// "included from" lines.
if (LastWarningLoc != PLoc.getIncludeLoc()) {
LastWarningLoc = PLoc.getIncludeLoc();
PrintIncludeStack(LastWarningLoc, SM);
}
if (DiagOpts->ShowLocation) {
// Emit the file/line/column that this expansion came from.
OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':';
if (DiagOpts->ShowColumn)
OS << PLoc.getColumn() << ':';
OS << ' ';
}
OS << "note: instantiated from:\n";
EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns,
OnMacroInst + 1, MacroSkipStart, MacroSkipEnd);
return;
}
if (DiagOpts->ShowLocation) {
// Emit the file/line/column that this expansion came from.
OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':';
if (DiagOpts->ShowColumn)
OS << PLoc.getColumn() << ':';
OS << ' ';
if (OnMacroInst == MacroSkipStart) {
// Tell the user that we've skipped contexts.
OS << "note: (skipping " << (MacroSkipEnd - MacroSkipStart)
<< " contexts in backtrace; use -fmacro-backtrace-limit=0 to see "
"all)\n";
}
OS << "note: instantiated from:\n";
EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns);
return;
}
@ -866,10 +887,29 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
}
}
unsigned MacroInstSkipStart = 0, MacroInstSkipEnd = 0;
if (DiagOpts && DiagOpts->MacroBacktraceLimit && !LastLoc.isFileID()) {
// Compute the length of the macro-instantiation backtrace, so that we
// can establish which steps in the macro backtrace we'll skip.
SourceLocation Loc = LastLoc;
unsigned Depth = 0;
do {
++Depth;
Loc = LastLoc.getManager().getImmediateInstantiationRange(Loc).first;
} while (!Loc.isFileID());
if (Depth > DiagOpts->MacroBacktraceLimit) {
MacroInstSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
DiagOpts->MacroBacktraceLimit % 2;
MacroInstSkipEnd = Depth - DiagOpts->MacroBacktraceLimit / 2;
}
}
EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
Info.getFixItHints(),
Info.getNumFixItHints(),
DiagOpts->MessageLength);
DiagOpts->MessageLength,
0, MacroInstSkipStart, MacroInstSkipEnd);
}
OS.flush();

View File

@ -0,0 +1,32 @@
// RUN: %clang-cc1 -fsyntax-only -fmacro-backtrace-limit 5 %s > %t 2>&1
// RUN: FileCheck %s < %t
#define M1(A, B) ((A) < (B))
#define M2(A, B) M1(A, B)
#define M3(A, B) M2(A, B)
#define M4(A, B) M3(A, B)
#define M5(A, B) M4(A, B)
#define M6(A, B) M5(A, B)
#define M7(A, B) M6(A, B)
#define M8(A, B) M7(A, B)
#define M9(A, B) M8(A, B)
#define M10(A, B) M9(A, B)
#define M11(A, B) M10(A, B)
#define M12(A, B) M11(A, B)
void f(int *ip, float *fp) {
// CHECK: macro-backtrace-limit.c:31:7: warning: comparison of distinct pointer types ('int *' and 'float *')
// CHECK: if (M12(ip, fp)) { }
// CHECK: macro-backtrace-limit.c:15:19: note: instantiated from:
// CHECK: #define M12(A, B) M11(A, B)
// CHECK: macro-backtrace-limit.c:14:19: note: instantiated from:
// CHECK: #define M11(A, B) M10(A, B)
// CHECK: note: (skipping 7 contexts in backtrace; use -fmacro-backtrace-limit=0 to see all)
// CHECK: macro-backtrace-limit.c:6:18: note: instantiated from:
// CHECK: #define M3(A, B) M2(A, B)
// CHECK: macro-backtrace-limit.c:5:18: note: instantiated from:
// CHECK: #define M2(A, B) M1(A, B)
// CHECK: macro-backtrace-limit.c:4:23: note: instantiated from:
// CHECK: #define M1(A, B) ((A) < (B))
if (M12(ip, fp)) { }
}