Sync with old GCOV runtime library's file.
authorBill Wendling <isanbard@gmail.com>
Mon, 28 May 2012 02:34:34 +0000 (02:34 +0000)
committerBill Wendling <isanbard@gmail.com>
Mon, 28 May 2012 02:34:34 +0000 (02:34 +0000)
llvm-svn: 157559

compiler-rt/lib/profile/GCDAProfiling.c

index fd506e9..3774b6d 100644 (file)
@@ -49,10 +49,8 @@ static void write_int32(uint32_t i) {
 }
 
 static void write_int64(uint64_t i) {
-  uint32_t lo, hi;
-  lo = i >>  0;
-  hi = i >> 32;
-
+  uint32_t lo = i >>  0;
+  uint32_t hi = i >> 32;
   write_int32(lo);
   write_int32(hi);
 }
@@ -91,17 +89,16 @@ static void recursive_mkdir(const char *filename) {
   int i, e;
 
   for (i = 1, e = strlen(filename); i != e; ++i) {
-    if (filename[i] == '/') {
-      pathname = malloc(i + 1);
-      strncpy(pathname, filename, i);
-      pathname[i] = '\0';
+    if (filename[i] != '/') continue;
+    pathname = malloc(i + 1);
+    strncpy(pathname, filename, i);
+    pathname[i] = '\0';
 #ifdef _WIN32
-      _mkdir(pathname);
+    _mkdir(pathname);
 #else
-      mkdir(pathname, 0750);  /* some of these will fail, ignore it. */
+    mkdir(pathname, 0750);  /* some of these will fail, ignore it. */
 #endif
-      free(pathname);
-    }
+    free(pathname);
   }
 }
 
@@ -117,7 +114,18 @@ void llvm_gcda_start_file(const char *orig_filename) {
   char *filename;
   filename = mangle_filename(orig_filename);
   recursive_mkdir(filename);
-  output_file = fopen(filename, "wb");
+  output_file = fopen(filename, "w+b");
+
+  if (!output_file) {
+    const char *cptr = strrchr(orig_filename, '/');
+    output_file = fopen(cptr ? cptr + 1 : orig_filename, "w+b");
+
+    if (!output_file) {
+      fprintf(stderr, "profiling:%s: cannot open\n",
+              cptr ? cptr + 1 : orig_filename);
+      return;
+    }
+  }
 
   /* gcda file, version 404*, stamp LLVM. */
 #ifdef __APPLE__
@@ -161,6 +169,7 @@ void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
 #ifdef DEBUG_GCDAPROFILING
   printf("llvmgcda: function id=%x\n", ident);
 #endif
+  if (!output_file) return;
 
   /* function tag */  
   fwrite("\0\0\0\1", 4, 1, output_file);
@@ -173,23 +182,24 @@ void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
 
 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
   uint32_t i;
-  /* counter #1 (arcs) tag */
+
+  /* Counter #1 (arcs) tag */
+  if (!output_file) return;
   fwrite("\0\0\xa1\1", 4, 1, output_file);
   write_int32(num_counters * 2);
-  for (i = 0; i < num_counters; ++i) {
+  for (i = 0; i < num_counters; ++i)
     write_int64(counters[i]);
-  }
 
 #ifdef DEBUG_GCDAPROFILING
   printf("llvmgcda:   %u arcs\n", num_counters);
-  for (i = 0; i < num_counters; ++i) {
+  for (i = 0; i < num_counters; ++i)
     printf("llvmgcda:   %llu\n", (unsigned long long)counters[i]);
-  }
 #endif
 }
 
 void llvm_gcda_end_file() {
   /* Write out EOF record. */
+  if (!output_file) return;
   fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
   fclose(output_file);
   output_file = NULL;