Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9a4fd2d
Move -fno-system-debug option to DebugOptions.def
Dec 9, 2023
04eeba8
Do not implement unnecessary -fsystem-debug
Dec 6, 2023
2a54ad0
Also do not generate debug info for system declarations if they are n…
Dec 7, 2023
9c55626
Also restrict generation of debug info for namespace aliases in syste…
Dec 7, 2023
f6fc3ac
Be more conservative about eliminating debug info for unreferenced de…
Dec 7, 2023
f20396f
Apply clang-format
Dec 7, 2023
0bf8904
Do annotate file as a system file since this can reduce debug info ge…
Dec 9, 2023
82233d0
Use correct option macro
Dec 9, 2023
e3368b9
Address formatting feedback
Dec 11, 2023
49112e7
Add testcase exercising non-generation of system header debug info
Dec 12, 2023
f6a0226
Generate for x86_64-unknown-linux since test checks for mangled names
Dec 12, 2023
6fca30e
Remove unnecessary blank line
Dec 12, 2023
ad8524d
Test when a templated class in system header is explicitly instantiat…
Dec 12, 2023
ae117d5
Use clangxx driver
Dec 12, 2023
66e991c
Replace all instances of %clang with %clangxx
Dec 12, 2023
de7b43b
Check for Windows mangled function names
Dec 12, 2023
6d01798
Fix Windows match strings
Dec 13, 2023
76eb7a9
Also generate debug info if decl isUsed. Make comments clearer.
Dec 14, 2023
a8115c9
isReferenced is always true if isUsed
Dec 14, 2023
73f8108
Add comment explaining why unreferenced explicit instantiations still…
Dec 14, 2023
e9e7dde
Rename cc1 option to -fno-system-debug to be same as the driver
Dec 14, 2023
132d9a3
Do not rely on IsReferenced for Using*Decls. It is not calculated.
Dec 15, 2023
94a7f2e
Enhance testcase to also check -fno-system-debug.
Dec 15, 2023
8094f9c
Apply clang-format
Dec 15, 2023
eccdd8b
Windows does not generate debug info for sin. Adapt test to this.
Dec 15, 2023
b207bea
Only stop debug generation if -fno-system-debug is used.
Dec 18, 2023
73ff530
Apply clang-format
Dec 18, 2023
557fe11
Debug generation not modified without -fno-system-debug. Update test…
Dec 18, 2023
048c1e0
Old tests do not need to adapt to this change since all changes are g…
Jan 8, 2024
7a5c985
Do not run debug-sin.cpp on Windows since Windows does not generate d…
Jan 8, 2024
321c2fd
More cleanly handle different expected mangled names for Linux/Windows.
Jan 8, 2024
227afc2
Explain purpose of test more clearly.
Jan 8, 2024
92c6995
Use correct pre-defined macro.
Jan 9, 2024
e96307c
Add driver test
Jan 9, 2024
7f1c06a
Remove unnecessary newlines
Jan 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DebugOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ ENUM_DEBUGOPT(DebugInfo, llvm::codegenoptions::DebugInfoKind, 4,
/// Whether to generate macro debug info.
DEBUGOPT(MacroDebugInfo, 1, 0)

/// Whether to not generate debug info for system headers.
DEBUGOPT(NoSystemDebug, 1, 0)

/// Tune the debug info for this debugger.
ENUM_DEBUGOPT(DebuggerTuning, llvm::DebuggerKind, 3,
llvm::DebuggerKind::Default)
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3761,6 +3761,10 @@ def fdebug_macro : Flag<["-"], "fdebug-macro">, Group<f_Group>,
def fno_debug_macro : Flag<["-"], "fno-debug-macro">, Group<f_Group>,
Visibility<[ClangOption, CLOption, DXCOption]>,
HelpText<"Do not emit macro debug information">;
def fno_system_debug : Flag<["-"], "fno-system-debug">, Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
HelpText<"Do not emit debug information for declarations in system headers">,
MarshallingInfoFlag<CodeGenOpts<"NoSystemDebug">>;
def fstrict_aliasing : Flag<["-"], "fstrict-aliasing">, Group<f_Group>,
Visibility<[ClangOption, CLOption, DXCOption]>,
HelpText<"Enable optimizations based on strict aliasing rules">;
Expand Down
46 changes: 34 additions & 12 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
return Src;

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

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

if (Ty->getDecl()->hasAttr<NoDebugAttr>())
if (Ty->getDecl()->hasAttr<NoDebugAttr>() ||
noSystemDebugInfo(Ty->getDecl(), CGM))
return Underlying;

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

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

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

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

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

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

if (D->hasAttr<NoDebugAttr>())
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
return;

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

if (Builder.GetInsertBlock() == nullptr)
return;
if (VD->hasAttr<NoDebugAttr>())
if (VD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(VD, CGM))
return;

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

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

void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
if (VD->hasAttr<NoDebugAttr>())
if (VD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(VD, CGM))
return;
llvm::TimeTraceScope TimeScope("DebugConstGlobalVariable", [&]() {
return GetName(VD, true);
Expand Down Expand Up @@ -5663,7 +5666,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
const VarDecl *D) {
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
if (D->hasAttr<NoDebugAttr>())
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
return;

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

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

auto AliaseeDecl = CGM.getMangledNameDecl(GV->getName());
Expand Down Expand Up @@ -5745,6 +5748,8 @@ llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
return;
if (noSystemDebugInfo(&UD, CGM))
return;
const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
if (!NSDecl->isAnonymousNamespace() ||
CGM.getCodeGenOpts().DebugExplicitImport) {
Expand All @@ -5770,6 +5775,8 @@ void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
return;
if (noSystemDebugInfo(&UD, CGM))
return;
assert(UD.shadow_size() &&
"We shouldn't be codegening an invalid UsingDecl containing no decls");

Expand All @@ -5795,6 +5802,8 @@ void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
void CGDebugInfo::EmitUsingEnumDecl(const UsingEnumDecl &UD) {
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
return;
if (noSystemDebugInfo(&UD, CGM))
return;
assert(UD.shadow_size() &&
"We shouldn't be codegening an invalid UsingEnumDecl"
" containing no decls");
Expand All @@ -5820,6 +5829,8 @@ llvm::DIImportedEntity *
CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
return nullptr;
if (noSystemDebugInfo(&NA, CGM))
return nullptr;
auto &VH = NamespaceAliasCache[&NA];
if (VH)
return cast<llvm::DIImportedEntity>(VH);
Expand Down Expand Up @@ -6011,3 +6022,14 @@ CGDebugInfo::createConstantValueExpression(const clang::ValueDecl *VD,

return nullptr;
}

bool clang::CodeGen::noSystemDebugInfo(const Decl *D,
const CodeGenModule &CGM) {
// Declaration is in system file
if (CGM.getContext().getSourceManager().isInSystemHeader(D->getLocation())) {
// -fno-system-debug was used. Do not generate debug info.
if (CGM.getCodeGenOpts().NoSystemDebug)
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/CGDebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ class ApplyInlineDebugLocation {
~ApplyInlineDebugLocation();
};

bool noSystemDebugInfo(const Decl *D, const CodeGenModule &CGM);

} // namespace CodeGen
} // namespace clang

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
llvm::GlobalVariable *Addr,
bool PerformInit) {
// Check if we need to emit debug info for variable initializer.
if (D->hasAttr<NoDebugAttr>())
if (D->hasAttr<NoDebugAttr>() || noSystemDebugInfo(D, CGM))
DebugInfo = nullptr; // disable debug info indefinitely for this function

CurEHLocation = D->getBeginLoc();
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,9 @@ void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
const ObjCContainerDecl *CD) {
SourceLocation StartLoc = OMD->getBeginLoc();
FunctionArgList args;

// Check if we should generate debug info for this method.
if (OMD->hasAttr<NoDebugAttr>())
if (OMD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(OMD, CGM))
DebugInfo = nullptr; // disable debug info indefinitely for this function

llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
}

// Check if we should generate debug info for this function.
if (FD->hasAttr<NoDebugAttr>()) {
if (FD->hasAttr<NoDebugAttr>() || noSystemDebugInfo(FD, CGM)) {
// Clear non-distinct debug info that was possibly attached to the function
// due to an earlier declaration without the nodebug attribute
Fn->setSubprogram(nullptr);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4564,6 +4564,10 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
D, TC))
CmdArgs.push_back("-debug-info-macro");

// -fno-system-debug turns off debug info generation for system headers
if (Args.hasArg(options::OPT_fno_system_debug))
CmdArgs.push_back("-fno-system-debug");

// -ggnu-pubnames turns on gnu style pubnames in the backend.
const auto *PubnamesArg =
Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
Expand Down
33 changes: 33 additions & 0 deletions clang/test/CodeGenCXX/debug-sin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////////////

// With default options, ensure that declarations obtained by a using declaration have
// debug info generated. sin() is declared through cmath with: using ::sin;
//
// Also ensure that no debug info for sin() is generated if -fno-system-debug is used.

// Debug info for math library functions is not generated on Windows
// UNSUPPORTED: system-windows

// RUN: %clang -emit-llvm -S -g %s -o %t.default.ll
// RUN: %clang -fno-system-debug -emit-llvm -S -g %s -o %t.no_system_debug.ll

// Check for debug info for "sin" with default option
// RUN: FileCheck --check-prefix=CHECK-DEFAULT %s < %t.default.ll

// No debug information for "sin" should be generated with -fno-system-debug
// RUN: FileCheck --check-prefix=CHECK-NO-SYSTEM-DEBUG %s < %t.no_system_debug.ll

// CHECK-DEFAULT: DISubprogram(name: "sin",
// CHECK-NO-SYSTEM-DEBUG-NOT: DISubprogram(name: "sin",

////////////////////////////////////////////////////////////////////////////////////////


#include <math.h>

int main() {
float f;

f=sin(1.32);
return 0;
}
Loading