Coverage mapping issue: Malformed profile data

Hi all!

It seems I came across on issue with coverage mapping
(http://www.llvm.org/docs/CoverageMappingFormat.html)

check on:
llvm revision: r228136
clang Last Changed Rev: 228121
build: Debug+Asserts
OS: ubuntu 14.04

Here is simple snippets

test1.c: NOT OK

Dmitry Volyntsev <xeioexception@gmail.com> writes:

It seems I came across on issue with coverage mapping
(http://www.llvm.org/docs/CoverageMappingFormat.html)

check on:
llvm revision: r228136
clang Last Changed Rev: 228121
build: Debug+Asserts
OS: ubuntu 14.04

Here is simple snippets

test1.c: NOT OK

#include <stdio.h>

static int foo() { return 42; }
int main() {
return 0;
}

cp src/test1.c src/test.c; rm -fr default.profraw test.profdata ;
clang -c -o obj/test.o src/test.c -I -g -O0 -fprofile-instr-generate
-fcoverage-mapping; clang -o bin/test obj/test.o -I -g -O0
-fprofile-instr-generate -fcoverage-mapping; ./bin/test ;
llvm-profdata merge -o test.profdata default.profraw; llvm-cov show
./bin/test -instr-profile=test.profdata

error: Failed to load coverage: Malformed profile data

test2.c: OK

#include <stdio.h>

static int foo() { return 42; }
int main() {
return foo();
}

cp src/test2.c src/test.c; rm -fr default.profraw test.profdata ;
clang -c -o obj/test.o src/test.c -I -g -O0 -fprofile-instr-generate
-fcoverage-mapping; clang -o bin/test obj/test.o -I -g -O0
-fprofile-instr-generate -fcoverage-mapping; ./bin/test ;
llvm-profdata merge -o test.profdata default.profraw; llvm-cov show
./bin/test -instr-profile=test.profdata

/home/xeioex/workspace/clang/experiments/llvm-cov/test/src/test.c:
       > 1|#include <stdio.h>
       > 2|
      1| 3|static int foo() { return 42; }
      1| 4|int main() {
      1| 5| return foo();
      1| 6|}

test3.c: OK

#include <stdio.h>

int foo() { return 42; }
int main() {
return 0;
}

cp src/test3.c src/test.c; rm -fr default.profraw test.profdata ;
clang -c -o obj/test.o src/test.c -I -g -O0 -fprofile-instr-generate
-fcoverage-mapping; clang -o bin/test obj/test.o -I -g -O0
-fprofile-instr-generate -fcoverage-mapping; ./bin/test ;
llvm-profdata merge -o test.profdata default.profraw; llvm-cov show
./bin/test -instr-profile=test.profdata

/home/xeioex/workspace/clang/experiments/llvm-cov/test/src/test.c:
       > 1|#include <stdio.h>
       > 2|
      0| 3|int foo() { return 42; }
      1| 4|int main() {
      1| 5| return 0;
      1| 6|}

As far I can understand issue somehow related to unused static functions.

It looks like this hits when a function isn't emitted by the frontend
but we emit an "empty" coverage map for it.

Also, as I can track down the problem raised because of
lib/ProfileData/CoverageMappingReader.cpp (SectionData::get) expects
that function's names located inside __llvm_prf_names section but it
not true in case of test1.c, instead some function names located
inside .rodata section.

The problem is that these don't go through the InstrProfiling machinery,
so the names aren't lowered properly. I'd noticed a related issue not
long ago, but fixed a bug by inspection that happened to hide it
(r226764).

Feel free to file a bug (http://llvm.org/bugs/) and I'll to get this
fixed in the next day or two.

Here is the bug: http://llvm.org/bugs/show_bug.cgi?id=22531

Here is probably a related bug: http://llvm.org/bugs/show_bug.cgi?id=22575