Skip to content

Commit

Permalink
[CodeGen] Add to emitted DebugLoc information about coverage when it'…
Browse files Browse the repository at this point in the history
…s required

Summary:
Some lines have a hit counter where they should not have one.
Cleanup stuff is located to the last line of the body which is most of the time a '}'.
And Exception stuff is added at the beginning of a function and at the end (represented by '{' and '}').
So in such cases, the DebugLoc used in GCOVProfiling.cpp must be marked as not covered.
This patch is a followup of https://reviews.llvm.org/D49915.
Tests in projects/compiler_rt are fixed by: https://reviews.llvm.org/D49917

Reviewers: marco-c, davidxl

Reviewed By: marco-c

Subscribers: dblaikie, cfe-commits, sylvestre.ledru

Differential Revision: https://reviews.llvm.org/D49916

llvm-svn: 342717
  • Loading branch information
calixteman committed Sep 21, 2018
1 parent eaf601a commit 5713db4
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 32 deletions.
26 changes: 15 additions & 11 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Expand Up @@ -76,20 +76,22 @@ CGDebugInfo::~CGDebugInfo() {
}

ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
SourceLocation TemporaryLocation)
SourceLocation TemporaryLocation,
bool ImplicitCode)
: CGF(&CGF) {
init(TemporaryLocation);
init(TemporaryLocation, false /* DefaultToEmpty */, ImplicitCode);
}

ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
bool DefaultToEmpty,
SourceLocation TemporaryLocation)
SourceLocation TemporaryLocation,
bool ImplicitCode)
: CGF(&CGF) {
init(TemporaryLocation, DefaultToEmpty);
init(TemporaryLocation, DefaultToEmpty, ImplicitCode);
}

void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
bool DefaultToEmpty) {
bool DefaultToEmpty, bool ImplicitCode) {
auto *DI = CGF->getDebugInfo();
if (!DI) {
CGF = nullptr;
Expand All @@ -102,7 +104,7 @@ void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
return;

if (TemporaryLocation.isValid()) {
DI->EmitLocation(CGF->Builder, TemporaryLocation);
DI->EmitLocation(CGF->Builder, TemporaryLocation, ImplicitCode);
return;
}

Expand Down Expand Up @@ -3484,16 +3486,18 @@ void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
}

void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
bool ImplicitCode) {
// Update our current location
setLocation(Loc);

if (CurLoc.isInvalid() || CurLoc.isMacroID())
return;

llvm::MDNode *Scope = LexicalBlockStack.back();
Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Builder.SetCurrentDebugLocation(
llvm::DebugLoc::get(getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope,
CurInlinedAt, ImplicitCode));
}

void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Expand Down Expand Up @@ -3540,7 +3544,7 @@ void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");

// Provide an entry in the line table for the end of the block.
EmitLocation(Builder, Loc);
EmitLocation(Builder, Loc, true /* ImplicitCode */);

if (DebugKind <= codegenoptions::DebugLineTablesOnly)
return;
Expand All @@ -3556,7 +3560,7 @@ void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
// Pop all regions for this function.
while (LexicalBlockStack.size() != RCount) {
// Provide an entry in the line table for the end of the block.
EmitLocation(Builder, CurLoc);
EmitLocation(Builder, CurLoc, true /* ImplicitCode */);
LexicalBlockStack.pop_back();
}
FnBeginRegionCount.pop_back();
Expand Down
19 changes: 13 additions & 6 deletions clang/lib/CodeGen/CGDebugInfo.h
Expand Up @@ -377,7 +377,9 @@ class CGDebugInfo {
/// Emit metadata to indicate a change in line/column information in
/// the source file. If the location is invalid, the previous
/// location will be reused.
void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
/// \param ImplicitCode True if the Loc must have coverage information
void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
bool ImplicitCode = false);

/// Emit a call to llvm.dbg.function.start to indicate
/// start of a new function.
Expand Down Expand Up @@ -664,16 +666,19 @@ class CGDebugInfo {
/// location or preferred location of the specified Expr.
class ApplyDebugLocation {
private:
void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false,
bool ImplicitCode = false);
ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
SourceLocation TemporaryLocation);
SourceLocation TemporaryLocation,
bool ImplicitCode = false);

llvm::DebugLoc OriginalLocation;
CodeGenFunction *CGF;

public:
/// Set the location to the (valid) TemporaryLocation.
ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation,
bool ImplicitCode = false);
ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) {
Expand All @@ -696,13 +701,15 @@ class ApplyDebugLocation {
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
return ApplyDebugLocation(CGF, false, SourceLocation());
}

/// Apply TemporaryLocation if it is valid. Otherwise switch
/// to an artificial debug location that has a valid scope, but no
/// line information.
static ApplyDebugLocation
CreateDefaultArtificial(CodeGenFunction &CGF,
SourceLocation TemporaryLocation) {
return ApplyDebugLocation(CGF, false, TemporaryLocation);
SourceLocation TemporaryLocation,
bool ImplicitCode = false) {
return ApplyDebugLocation(CGF, false, TemporaryLocation, ImplicitCode);
}

/// Set the IRBuilder to not attach debug locations. Note that
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGException.cpp
Expand Up @@ -767,7 +767,8 @@ llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {

// Save the current IR generation state.
CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);
auto DL = ApplyDebugLocation::CreateDefaultArtificial(
*this, CurEHLocation, true /* ImplicitCode */);

// Create and configure the landing pad.
llvm::BasicBlock *lpad = createBasicBlock("lpad");
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Expand Up @@ -317,7 +317,7 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
if (OnlySimpleReturnStmts)
DI->EmitLocation(Builder, LastStopPoint);
else
DI->EmitLocation(Builder, EndLoc);
DI->EmitLocation(Builder, EndLoc, true /* ImplicitCode */);
}

// Pop any cleanups that might have been associated with the
Expand All @@ -333,7 +333,7 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
// the ret after it's been at EndLoc.
if (CGDebugInfo *DI = getDebugInfo())
if (OnlySimpleReturnStmts)
DI->EmitLocation(Builder, EndLoc);
DI->EmitLocation(Builder, EndLoc, true /* ImplicitCode */);

PopCleanupBlocks(PrologueCleanupDepth);
}
Expand Down Expand Up @@ -1179,7 +1179,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
}
// Emit a location at the end of the prologue.
if (CGDebugInfo *DI = getDebugInfo())
DI->EmitLocation(Builder, StartLoc);
DI->EmitLocation(Builder, StartLoc, true /* ImplicitCode */);

// TODO: Do we need to handle this in two places like we do with
// target-features/target-cpu?
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Expand Up @@ -786,7 +786,7 @@ class CodeGenFunction : public CodeGenTypeCache {
// If we should perform a cleanup, force them now. Note that
// this ends the cleanup scope before rescoping any labels.
if (PerformCleanup) {
ApplyDebugLocation DL(CGF, Range.getEnd());
ApplyDebugLocation DL(CGF, Range.getEnd(), true /* ImplicitCode */);
ForceCleanup();
}
}
Expand Down
9 changes: 5 additions & 4 deletions clang/test/CodeGen/debug-info-scope-file.c
Expand Up @@ -5,10 +5,11 @@

// CHECK: ret void, !dbg [[F1_LINE:![0-9]*]]
// CHECK: ret void, !dbg [[F2_LINE:![0-9]*]]
// CHECK: [[F1:![0-9]*]] = distinct !DISubprogram(name: "f1",{{.*}} isDefinition: true
// CHECK: [[F1_LINE]] = !DILocation({{.*}}, scope: [[F1]])
// CHECK: [[F2:![0-9]*]] = distinct !DISubprogram(name: "f2",{{.*}} isDefinition: true
// CHECK: [[F2_LINE]] = !DILocation({{.*}}, scope: [[F2]])
// CHECK: [[F1:![0-9]*]] = distinct !DISubprogram(name: "f1",{{.*}}
// isDefinition: true CHECK: [[F1_LINE]] = !DILocation({{.*}}, scope: [[F1]],
// isImplicitCode: true) CHECK: [[F2:![0-9]*]] = distinct !DISubprogram(name:
// "f2",{{.*}} isDefinition: true CHECK: [[F2_LINE]] = !DILocation({{.*}},
// scope: [[F2]], isImplicitCode: true)

void f1() {
}
Expand Down
Expand Up @@ -21,5 +21,5 @@ void foo() {
// CHECK-DAG: ![[LOC]] = !DILocation(line: 0, scope: ![[A]], inlinedAt: ![[INL:[0-9]+]])
// CHECK-DAG: ![[INL]] = !DILocation(line: [[@LINE+1]], scope: ![[FOO]])
B b(0);
// CHECK: ![[NOINL]] = !DILocation(line: [[@LINE+1]], scope: !{{[0-9]+}})
// CHECK: ![[NOINL]] = !DILocation(line: [[@LINE+1]], scope: !{{[0-9]+}}, isImplicitCode: true)
}
4 changes: 2 additions & 2 deletions clang/test/CodeGenCXX/linetable-virtual-variadic.cpp
Expand Up @@ -17,6 +17,6 @@ void Derived::VariadicFunction(...) { }
// CHECK: ret void, !dbg ![[LOC_I:[0-9]+]]
//
// CHECK: ![[SP]] = distinct !DISubprogram(name: "VariadicFunction"
// CHECK: ![[LOC]] = !DILocation({{.*}}scope: ![[SP]])
// CHECK: ![[LOC]] = !DILocation({{.*}}scope: ![[SP]], isImplicitCode: true)
// CHECK: ![[SP_I]] = distinct !DISubprogram(name: "VariadicFunction"
// CHECK: ![[LOC_I]] = !DILocation({{.*}}scope: ![[SP_I]])
// CHECK: ![[LOC_I]] = !DILocation({{.*}}scope: ![[SP_I]], isImplicitCode: true)
2 changes: 1 addition & 1 deletion clang/test/CodeGenObjC/arc-linetable.m
Expand Up @@ -60,7 +60,7 @@ @implementation AppDelegate : NSObject
- (int)testNoSideEffect:(NSString *)foo {
int x = 1;
return 1; // Return expression
// CHECK: ![[RET1]] = !DILocation(line: [[@LINE+1]], scope: ![[TESTNOSIDEEFFECT]])
// CHECK: ![[RET1]] = !DILocation(line: [[@LINE+1]], scope: ![[TESTNOSIDEEFFECT]], isImplicitCode: true)
} // Cleanup + Ret

- (int)testNoCleanup {
Expand Down
2 changes: 0 additions & 2 deletions clang/test/CodeGenObjC/debug-info-blocks.m
Expand Up @@ -18,11 +18,9 @@
// CHECK: call {{.*}}, !dbg ![[DBG_LINE:[0-9]+]]
// CHECK-NOT: ret
// CHECK: load {{.*}}, !dbg ![[COPY_LINE:[0-9]+]]
// CHECK: ret void, !dbg ![[COPY_LINE]]
// CHECK: define {{.*}} @__destroy_helper_block_{{.*}}(i8*)
// CHECK-NOT: ret
// CHECK: load {{.*}}, !dbg ![[DESTROY_LINE:[0-9]+]]
// CHECK: ret void, !dbg ![[DESTROY_LINE]]

typedef unsigned int NSUInteger;

Expand Down

0 comments on commit 5713db4

Please sign in to comment.