Skip to content

Commit 24255a5

Browse files
authored
Do not generate debug info for declarations in system header files (intel#11544)
Add option: -fno-system-debug Do not emit debug information for declarations in system headers --------- Signed-off-by: Lu, John <john.lu@intel.com>
1 parent 840ed4d commit 24255a5

File tree

11 files changed

+204
-15
lines changed

11 files changed

+204
-15
lines changed

clang/include/clang/Basic/DebugOptions.def

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ ENUM_DEBUGOPT(DebugInfo, llvm::codegenoptions::DebugInfoKind, 4,
108108
/// Whether to generate macro debug info.
109109
DEBUGOPT(MacroDebugInfo, 1, 0)
110110

111+
/// Whether to not generate debug info for system headers.
112+
DEBUGOPT(NoSystemDebug, 1, 0)
113+
111114
/// Tune the debug info for this debugger.
112115
ENUM_DEBUGOPT(DebuggerTuning, llvm::DebuggerKind, 3,
113116
llvm::DebuggerKind::Default)

clang/include/clang/Driver/Options.td

+4
Original file line numberDiff line numberDiff line change
@@ -3769,6 +3769,10 @@ def fdebug_macro : Flag<["-"], "fdebug-macro">, Group<f_Group>,
37693769
def fno_debug_macro : Flag<["-"], "fno-debug-macro">, Group<f_Group>,
37703770
Visibility<[ClangOption, CLOption, DXCOption]>,
37713771
HelpText<"Do not emit macro debug information">;
3772+
def fno_system_debug : Flag<["-"], "fno-system-debug">, Group<f_Group>,
3773+
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
3774+
HelpText<"Do not emit debug information for declarations in system headers">,
3775+
MarshallingInfoFlag<CodeGenOpts<"NoSystemDebug">>;
37723776
def fstrict_aliasing : Flag<["-"], "fstrict-aliasing">, Group<f_Group>,
37733777
Visibility<[ClangOption, CLOption, DXCOption]>,
37743778
HelpText<"Enable optimizations based on strict aliasing rules">;

clang/lib/CodeGen/CGDebugInfo.cpp

+34-12
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
13761376
return Src;
13771377

13781378
const auto *AliasDecl = cast<TypeAliasTemplateDecl>(TD)->getTemplatedDecl();
1379-
if (AliasDecl->hasAttr<NoDebugAttr>())
1379+
if (AliasDecl->hasAttr<NoDebugAttr>() || noSystemDebugInfo(AliasDecl, CGM))
13801380
return Src;
13811381

13821382
SmallString<128> NS;
@@ -1435,7 +1435,8 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
14351435
llvm::DIType *Underlying =
14361436
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
14371437

1438-
if (Ty->getDecl()->hasAttr<NoDebugAttr>())
1438+
if (Ty->getDecl()->hasAttr<NoDebugAttr>() ||
1439+
noSystemDebugInfo(Ty->getDecl(), CGM))
14391440
return Underlying;
14401441

14411442
// We don't set size information, but do specify where the typedef was
@@ -1818,7 +1819,7 @@ void CGDebugInfo::CollectRecordFields(
18181819
// the corresponding declarations in the source program.
18191820
for (const auto *I : record->decls())
18201821
if (const auto *V = dyn_cast<VarDecl>(I)) {
1821-
if (V->hasAttr<NoDebugAttr>())
1822+
if (V->hasAttr<NoDebugAttr>() || noSystemDebugInfo(V, CGM))
18221823
continue;
18231824

18241825
// Skip variable template specializations when emitting CodeView. MSVC
@@ -2081,7 +2082,8 @@ void CGDebugInfo::CollectCXXMemberFunctions(
20812082
// derived classes. GDB doesn't seem to notice/leverage these when I tried
20822083
// it, so I'm not rushing to fix this. (GCC seems to produce them, if
20832084
// referenced)
2084-
if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
2085+
if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>() ||
2086+
noSystemDebugInfo(Method, CGM))
20852087
continue;
20862088

20872089
if (Method->getType()->castAs<FunctionProtoType>()->getContainedAutoType())
@@ -4451,6 +4453,7 @@ void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
44514453
// Do not emit a declaration subprogram for a function with nodebug
44524454
// attribute, or if call site info isn't required.
44534455
if (CalleeDecl->hasAttr<NoDebugAttr>() ||
4456+
noSystemDebugInfo(CalleeDecl, CGM) ||
44544457
getCallSiteRelatedAttrs() == llvm::DINode::FlagZero)
44554458
return;
44564459

@@ -4642,7 +4645,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
46424645
const bool UsePointerValue) {
46434646
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
46444647
assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
4645-
if (VD->hasAttr<NoDebugAttr>())
4648+
if (VD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(VD, CGM))
46464649
return nullptr;
46474650

46484651
bool Unwritten =
@@ -4856,7 +4859,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
48564859
const bool UsePointerValue) {
48574860
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
48584861
assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
4859-
if (BD->hasAttr<NoDebugAttr>())
4862+
if (BD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(BD, CGM))
48604863
return nullptr;
48614864

48624865
// Skip the tuple like case, we don't handle that here
@@ -4962,7 +4965,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
49624965
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
49634966
assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
49644967

4965-
if (D->hasAttr<NoDebugAttr>())
4968+
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
49664969
return;
49674970

49684971
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
@@ -5001,7 +5004,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
50015004

50025005
if (Builder.GetInsertBlock() == nullptr)
50035006
return;
5004-
if (VD->hasAttr<NoDebugAttr>())
5007+
if (VD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(VD, CGM))
50055008
return;
50065009

50075010
bool isByRef = VD->hasAttr<BlocksAttr>();
@@ -5521,7 +5524,7 @@ std::string CGDebugInfo::GetName(const Decl *D, bool Qualified) const {
55215524
void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
55225525
const VarDecl *D) {
55235526
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5524-
if (D->hasAttr<NoDebugAttr>())
5527+
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
55255528
return;
55265529

55275530
llvm::TimeTraceScope TimeScope("DebugGlobalVariable", [&]() {
@@ -5586,7 +5589,7 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
55865589

55875590
void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
55885591
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5589-
if (VD->hasAttr<NoDebugAttr>())
5592+
if (VD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(VD, CGM))
55905593
return;
55915594
llvm::TimeTraceScope TimeScope("DebugConstGlobalVariable", [&]() {
55925595
return GetName(VD, true);
@@ -5663,7 +5666,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
56635666
void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
56645667
const VarDecl *D) {
56655668
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5666-
if (D->hasAttr<NoDebugAttr>())
5669+
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
56675670
return;
56685671

56695672
auto Align = getDeclAlignIfRequired(D, CGM.getContext());
@@ -5688,7 +5691,7 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
56885691
return;
56895692

56905693
const auto *D = cast<ValueDecl>(GD.getDecl());
5691-
if (D->hasAttr<NoDebugAttr>())
5694+
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
56925695
return;
56935696

56945697
auto AliaseeDecl = CGM.getMangledNameDecl(GV->getName());
@@ -5745,6 +5748,8 @@ llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
57455748
void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
57465749
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
57475750
return;
5751+
if (noSystemDebugInfo(&UD, CGM))
5752+
return;
57485753
const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
57495754
if (!NSDecl->isAnonymousNamespace() ||
57505755
CGM.getCodeGenOpts().DebugExplicitImport) {
@@ -5770,6 +5775,8 @@ void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
57705775
void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
57715776
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
57725777
return;
5778+
if (noSystemDebugInfo(&UD, CGM))
5779+
return;
57735780
assert(UD.shadow_size() &&
57745781
"We shouldn't be codegening an invalid UsingDecl containing no decls");
57755782

@@ -5795,6 +5802,8 @@ void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
57955802
void CGDebugInfo::EmitUsingEnumDecl(const UsingEnumDecl &UD) {
57965803
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
57975804
return;
5805+
if (noSystemDebugInfo(&UD, CGM))
5806+
return;
57985807
assert(UD.shadow_size() &&
57995808
"We shouldn't be codegening an invalid UsingEnumDecl"
58005809
" containing no decls");
@@ -5820,6 +5829,8 @@ llvm::DIImportedEntity *
58205829
CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
58215830
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
58225831
return nullptr;
5832+
if (noSystemDebugInfo(&NA, CGM))
5833+
return nullptr;
58235834
auto &VH = NamespaceAliasCache[&NA];
58245835
if (VH)
58255836
return cast<llvm::DIImportedEntity>(VH);
@@ -6011,3 +6022,14 @@ CGDebugInfo::createConstantValueExpression(const clang::ValueDecl *VD,
60116022

60126023
return nullptr;
60136024
}
6025+
6026+
bool clang::CodeGen::noSystemDebugInfo(const Decl *D,
6027+
const CodeGenModule &CGM) {
6028+
// Declaration is in system file
6029+
if (CGM.getContext().getSourceManager().isInSystemHeader(D->getLocation())) {
6030+
// -fno-system-debug was used. Do not generate debug info.
6031+
if (CGM.getCodeGenOpts().NoSystemDebug)
6032+
return true;
6033+
}
6034+
return false;
6035+
}

clang/lib/CodeGen/CGDebugInfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,8 @@ class ApplyInlineDebugLocation {
901901
~ApplyInlineDebugLocation();
902902
};
903903

904+
bool noSystemDebugInfo(const Decl *D, const CodeGenModule &CGM);
905+
904906
} // namespace CodeGen
905907
} // namespace clang
906908

clang/lib/CodeGen/CGDeclCXX.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
10111011
llvm::GlobalVariable *Addr,
10121012
bool PerformInit) {
10131013
// Check if we need to emit debug info for variable initializer.
1014-
if (D->hasAttr<NoDebugAttr>())
1014+
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
10151015
DebugInfo = nullptr; // disable debug info indefinitely for this function
10161016

10171017
CurEHLocation = D->getBeginLoc();

clang/lib/CodeGen/CGObjC.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,9 @@ void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
753753
const ObjCContainerDecl *CD) {
754754
SourceLocation StartLoc = OMD->getBeginLoc();
755755
FunctionArgList args;
756+
756757
// Check if we should generate debug info for this method.
757-
if (OMD->hasAttr<NoDebugAttr>())
758+
if (OMD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(OMD, CGM))
758759
DebugInfo = nullptr; // disable debug info indefinitely for this function
759760

760761
llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);

clang/lib/CodeGen/CodeGenFunction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
16281628
}
16291629

16301630
// Check if we should generate debug info for this function.
1631-
if (FD->hasAttr<NoDebugAttr>()) {
1631+
if (FD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(FD, CGM)) {
16321632
// Clear non-distinct debug info that was possibly attached to the function
16331633
// due to an earlier declaration without the nodebug attribute
16341634
Fn->setSubprogram(nullptr);

clang/lib/Driver/ToolChains/Clang.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,10 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
45754575
D, TC))
45764576
CmdArgs.push_back("-debug-info-macro");
45774577

4578+
// -fno-system-debug turns off debug info generation for system headers
4579+
if (Args.hasArg(options::OPT_fno_system_debug))
4580+
CmdArgs.push_back("-fno-system-debug");
4581+
45784582
// -ggnu-pubnames turns on gnu style pubnames in the backend.
45794583
const auto *PubnamesArg =
45804584
Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,

clang/test/CodeGenCXX/debug-sin.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
////////////////////////////////////////////////////////////////////////////////////////
2+
3+
// With default options, ensure that declarations obtained by a using declaration have
4+
// debug info generated. sin() is declared through cmath with: using ::sin;
5+
//
6+
// Also ensure that no debug info for sin() is generated if -fno-system-debug is used.
7+
8+
// Debug info for math library functions is not generated on Windows
9+
// UNSUPPORTED: system-windows
10+
11+
// RUN: %clang -emit-llvm -S -g %s -o %t.default.ll
12+
// RUN: %clang -fno-system-debug -emit-llvm -S -g %s -o %t.no_system_debug.ll
13+
14+
// Check for debug info for "sin" with default option
15+
// RUN: FileCheck --check-prefix=CHECK-DEFAULT %s < %t.default.ll
16+
17+
// No debug information for "sin" should be generated with -fno-system-debug
18+
// RUN: FileCheck --check-prefix=CHECK-NO-SYSTEM-DEBUG %s < %t.no_system_debug.ll
19+
20+
// CHECK-DEFAULT: DISubprogram(name: "sin",
21+
// CHECK-NO-SYSTEM-DEBUG-NOT: DISubprogram(name: "sin",
22+
23+
////////////////////////////////////////////////////////////////////////////////////////
24+
25+
26+
#include <math.h>
27+
28+
int main() {
29+
float f;
30+
31+
f=sin(1.32);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)