llvm-profdata: Update to use the naive text format with function hash
authorJustin Bogner <mail@justinbogner.com>
Wed, 19 Mar 2014 02:20:42 +0000 (02:20 +0000)
committerJustin Bogner <mail@justinbogner.com>
Wed, 19 Mar 2014 02:20:42 +0000 (02:20 +0000)
This also uses line_iterator to simplify the parsing logic.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204210 91177308-0d34-0410-b5e6-96231b3b80d8

16 files changed:
include/llvm/Support/LineIterator.h
test/tools/llvm-profdata/Inputs/bad-function-count.profdata [deleted file]
test/tools/llvm-profdata/Inputs/bad-hash.profdata [new file with mode: 0644]
test/tools/llvm-profdata/Inputs/bar3-1.profdata
test/tools/llvm-profdata/Inputs/foo3-1.profdata
test/tools/llvm-profdata/Inputs/foo3-2.profdata
test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata
test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata
test/tools/llvm-profdata/Inputs/foo4-1.profdata
test/tools/llvm-profdata/Inputs/foo4-2.profdata
test/tools/llvm-profdata/Inputs/invalid-count-later.profdata
test/tools/llvm-profdata/Inputs/overflow.profdata
test/tools/llvm-profdata/Inputs/three-words-long.profdata [deleted file]
test/tools/llvm-profdata/errors.test
test/tools/llvm-profdata/simple.test
tools/llvm-profdata/llvm-profdata.cpp

index 861c19881fe3ac70e0128021b535e3f52b6042b2..92a9cc5c574a05125978345c6a81708ff8d6e893 100644 (file)
@@ -56,6 +56,7 @@ public:
 
   /// \brief Get the current line as a \c StringRef.
   StringRef operator*() const { return CurrentLine; }
+  const StringRef *operator->() const { return &CurrentLine; }
 
   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
     return LHS.Buffer == RHS.Buffer &&
diff --git a/test/tools/llvm-profdata/Inputs/bad-function-count.profdata b/test/tools/llvm-profdata/Inputs/bad-function-count.profdata
deleted file mode 100644 (file)
index 7d24762..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-function_count_not 1count
-1
diff --git a/test/tools/llvm-profdata/Inputs/bad-hash.profdata b/test/tools/llvm-profdata/Inputs/bad-hash.profdata
new file mode 100644 (file)
index 0000000..faa6f40
--- /dev/null
@@ -0,0 +1,4 @@
+function_count_not
+badhash
+1
+1
index cb8b409891a5a79d57a392cc5765410e9d8e26a0..5486e9d84d72c2ac4235d7e0906ceb148b8ffaac 100644 (file)
@@ -1,4 +1,6 @@
-bar 3
+bar
+3
+3
 1
 2
 3
index d6f9f648b7bfd7a6fed2b17d3c40ec139a6edabc..14a620043576e1deea37725cdd5a60ba93a19907 100644 (file)
@@ -1,4 +1,6 @@
-foo 3
+foo
+3
+3
 1
 2
 3
index 94fd0343915abb45b990eacfa9b7a91e759c6299..801846e440453606933271880db218195cf75122 100644 (file)
@@ -1,4 +1,6 @@
-foo 3
+foo
+3
+3
 7
 5
 3
index 85b702d497788cfde1a9dda4f0784a505f084da7..12157b9f9ada509a9fc2856f8c7272b2177d8c11 100644 (file)
@@ -1,9 +1,13 @@
-foo 3
+foo
+3
+3
 2
 3
 5
 
-bar 3
+bar
+3
+3
 7
 11
 13
index d652781a35c35b41e94daeb0fc84629e2a96bddc..f1f10bd6f3dfbd9569128d0fe3668947dc13d584 100644 (file)
@@ -1,9 +1,13 @@
-foo 3
+foo
+3
+3
 17
 19
 23
 
-bar 3
+bar
+3
+3
 29
 31
 37
index 4d694080bffa52576d9030701e1aa20aa2e842a3..31d2a2ce75695164bbd03e79debec08ff50676dd 100644 (file)
@@ -1,4 +1,6 @@
-foo 4
+foo
+4
+4
 11
 22
 33
index 8d91d8bbef0fc7378104d849f84795c57e648dfe..01d8309b5ce2eaf77283038c385d4c75170e1d84 100644 (file)
@@ -1,4 +1,6 @@
-foo 4
+foo
+4
+4
 7
 6
 5
index 5575df3fda66ef25eb0ebc4e70221564d2a3cdb9..2b61c55a67668b6324135e3e92adbd47eae80cfd 100644 (file)
@@ -1,2 +1,4 @@
-invalid_count 1
+invalid_count
+1
+1
 1later
index bfb9a52d2ec5b76d85122fccbce3babc6a53f6e5..c9a9d697ecfb1620af0a448e15336c7e54b3933e 100644 (file)
@@ -1,2 +1,4 @@
-overflow 1
+overflow
+1
+1
 9223372036854775808
diff --git a/test/tools/llvm-profdata/Inputs/three-words-long.profdata b/test/tools/llvm-profdata/Inputs/three-words-long.profdata
deleted file mode 100644 (file)
index a4d45fb..0000000
+++ /dev/null
@@ -1 +0,0 @@
-three words long
index 6335ea95f9ea24554c624cc7ef976c0ef02dc5d7..6570d172ee824b37560418f8eb039ccd752ce5d7 100644 (file)
@@ -6,8 +6,8 @@ LENGTH: error: {{.*}}: truncated file
 RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/bar3-1.profdata 2>&1 | FileCheck %s --check-prefix=NAME
 NAME: error: {{.*}}: function name mismatch
 
-RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=COUNT
-COUNT: error: {{.*}}: function count mismatch
+RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=HASH
+HASH: error: {{.*}}: function hash mismatch
 
 RUN: not llvm-profdata %p/Inputs/overflow.profdata %p/Inputs/overflow.profdata 2>&1 | FileCheck %s --check-prefix=OVERFLOW
 OVERFLOW: error: {{.*}}: counter overflow
@@ -15,8 +15,5 @@ OVERFLOW: error: {{.*}}: counter overflow
 RUN: not llvm-profdata %p/Inputs/invalid-count-later.profdata %p/Inputs/invalid-count-later.profdata 2>&1 | FileCheck %s --check-prefix=INVALID-COUNT-LATER
 INVALID-COUNT-LATER: error: {{.*}}: invalid counter
 
-RUN: not llvm-profdata %p/Inputs/bad-function-count.profdata %p/Inputs/bad-function-count.profdata 2>&1 | FileCheck %s --check-prefix=BAD-FUNCTION-COUNT
-BAD-FUNCTION-COUNT: error: {{.*}}: bad function count
-
-RUN: not llvm-profdata %p/Inputs/three-words-long.profdata %p/Inputs/three-words-long.profdata 2>&1 | FileCheck %s --check-prefix=INVALID-DATA
-INVALID-DATA: error: {{.*}}: invalid data
+RUN: not llvm-profdata %p/Inputs/bad-hash.profdata %p/Inputs/bad-hash.profdata 2>&1 | FileCheck %s --check-prefix=BAD-HASH
+BAD-HASH: error: {{.*}}: bad function hash
index 87073fad9cfa65f75ed28bf9970fc46b6edcb822..7e375bf4b47ac97e12bfbf4cb67e71c3a6eaa912 100644 (file)
@@ -1,13 +1,17 @@
 RUN: llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo3-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO3
 RUN: llvm-profdata %p/Inputs/foo3-2.profdata %p/Inputs/foo3-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO3
-FOO3:      {{^foo 3$}}
+FOO3:      {{^foo$}}
+FOO3-NEXT: {{^3$}}
+FOO3-NEXT: {{^3$}}
 FOO3-NEXT: {{^8$}}
 FOO3-NEXT: {{^7$}}
 FOO3-NEXT: {{^6$}}
 
 RUN: llvm-profdata %p/Inputs/foo4-1.profdata %p/Inputs/foo4-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO4
 RUN: llvm-profdata %p/Inputs/foo4-2.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO4
-FOO4:      {{^foo 4$}}
+FOO4:      {{^foo$}}
+FOO4-NEXT: {{^4$}}
+FOO4-NEXT: {{^4$}}
 FOO4-NEXT: {{^18$}}
 FOO4-NEXT: {{^28$}}
 FOO4-NEXT: {{^38$}}
@@ -15,11 +19,15 @@ FOO4-NEXT: {{^48$}}
 
 RUN: llvm-profdata %p/Inputs/foo3bar3-1.profdata %p/Inputs/foo3bar3-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO3BAR3
 RUN: llvm-profdata %p/Inputs/foo3bar3-2.profdata %p/Inputs/foo3bar3-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO3BAR3
-FOO3BAR3:      {{^foo 3$}}
+FOO3BAR3:      {{^foo$}}
+FOO3BAR3-NEXT: {{^3$}}
+FOO3BAR3-NEXT: {{^3$}}
 FOO3BAR3-NEXT: {{^19$}}
 FOO3BAR3-NEXT: {{^22$}}
 FOO3BAR3-NEXT: {{^28$}}
-FOO3BAR3:      {{^bar 3$}}
+FOO3BAR3:      {{^bar$}}
+FOO3BAR3-NEXT: {{^3$}}
+FOO3BAR3-NEXT: {{^3$}}
 FOO3BAR3-NEXT: {{^36$}}
 FOO3BAR3-NEXT: {{^42$}}
 FOO3BAR3-NEXT: {{^50$}}
index b338efd27e358d7e4cad22a000dcecd3f0fb38b9..b4dd55c833756fa0f292c6f93eb5d26f8229261c 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/LineIterator.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -32,56 +33,6 @@ static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
 static cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
                                  cl::aliasopt(OutputFilename));
 
-static bool readLine(const char *&Start, const char *End, StringRef &S) {
-  if (Start == End)
-    return false;
-
-  for (const char *I = Start; I != End; ++I) {
-    assert(*I && "unexpected binary data");
-    if (*I == '\n') {
-      S = StringRef(Start, I - Start);
-      Start = I + 1;
-      return true;
-    }
-  }
-
-  S = StringRef(Start, End - Start);
-  Start = End;
-  return true;
-}
-
-static StringRef getWord(const char *&Start, const char *End) {
-  for (const char *I = Start; I != End; ++I)
-    if (*I == ' ') {
-      StringRef S(Start, I - Start);
-      Start = I + 1;
-      return S;
-    }
-  StringRef S(Start, End - Start);
-  Start = End;
-  return S;
-}
-
-static size_t splitWords(const StringRef &Line, std::vector<StringRef> &Words) {
-  const char *Start = Line.data();
-  const char *End = Line.data() + Line.size();
-  Words.clear();
-  while (Start != End)
-    Words.push_back(getWord(Start, End));
-  return Words.size();
-}
-
-static bool getNumber(const StringRef &S, uint64_t &N) {
-  N = 0;
-  for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I)
-    if (*I >= '0' && *I <= '9')
-      N = N * 10 + (*I - '0');
-    else
-      return false;
-
-  return true;
-}
-
 static void exitWithError(const std::string &Message,
                           const std::string &Filename, int64_t Line = -1) {
   errs() << "error: " << Filename;
@@ -115,63 +66,64 @@ int main(int argc, char **argv) {
   if (!ErrorInfo.empty())
     exitWithError(ErrorInfo, OutputFilename);
 
-  const char *Start1 = File1->getBufferStart();
-  const char *Start2 = File2->getBufferStart();
-  const char *End1 = File1->getBufferEnd();
-  const char *End2 = File2->getBufferEnd();
-  const char *P1 = Start1;
-  const char *P2 = Start2;
-
-  StringRef Line1, Line2;
-  int64_t Num = 0;
-  while (readLine(P1, End1, Line1)) {
-    ++Num;
-    if (!readLine(P2, End2, Line2))
-      exitWithError("truncated file", Filename2, Num);
-
-    std::vector<StringRef> Words1, Words2;
-    if (splitWords(Line1, Words1) != splitWords(Line2, Words2))
-      exitWithError("data mismatch", Filename2, Num);
-
-    if (Words1.size() > 2)
-      exitWithError("invalid data", Filename1, Num);
-
-    if (Words1.empty()) {
+  enum {ReadName, ReadHash, ReadCount, ReadCounters} State = ReadName;
+  uint64_t N1, N2, NumCounters;
+  line_iterator I1(*File1, '#'), I2(*File2, '#');
+  for (; !I1.is_at_end() && !I2.is_at_end(); ++I1, ++I2) {
+    if (I1->empty()) {
+      if (!I2->empty())
+        exitWithError("data mismatch", Filename2, I2.line_number());
       Output << "\n";
       continue;
     }
-
-    if (Words1.size() == 2) {
-      if (Words1[0] != Words2[0])
-        exitWithError("function name mismatch", Filename2, Num);
-
-      uint64_t N1, N2;
-      if (!getNumber(Words1[1], N1))
-        exitWithError("bad function count", Filename1, Num);
-      if (!getNumber(Words2[1], N2))
-        exitWithError("bad function count", Filename2, Num);
-
+    switch (State) {
+    case ReadName:
+      if (*I1 != *I2)
+        exitWithError("function name mismatch", Filename2, I2.line_number());
+      Output << *I1 << "\n";
+      State = ReadHash;
+      break;
+    case ReadHash:
+      if (I1->getAsInteger(10, N1))
+        exitWithError("bad function hash", Filename1, I1.line_number());
+      if (I2->getAsInteger(10, N2))
+        exitWithError("bad function hash", Filename2, I2.line_number());
       if (N1 != N2)
-        exitWithError("function count mismatch", Filename2, Num);
-
-      Output << Line1 << "\n";
-      continue;
+        exitWithError("function hash mismatch", Filename2, I2.line_number());
+      Output << N1 << "\n";
+      State = ReadCount;
+      break;
+    case ReadCount:
+      if (I1->getAsInteger(10, N1))
+        exitWithError("bad function count", Filename1, I1.line_number());
+      if (I2->getAsInteger(10, N2))
+        exitWithError("bad function count", Filename2, I2.line_number());
+      if (N1 != N2)
+        exitWithError("function count mismatch", Filename2, I2.line_number());
+      Output << N1 << "\n";
+      NumCounters = N1;
+      State = ReadCounters;
+      break;
+    case ReadCounters:
+      if (I1->getAsInteger(10, N1))
+        exitWithError("invalid counter", Filename1, I1.line_number());
+      if (I2->getAsInteger(10, N2))
+        exitWithError("invalid counter", Filename2, I2.line_number());
+      uint64_t Sum = N1 + N2;
+      if (Sum < N1)
+        exitWithError("counter overflow", Filename2, I2.line_number());
+      Output << N1 + N2 << "\n";
+      if (--NumCounters == 0)
+        State = ReadName;
+      break;
     }
-
-    uint64_t N1, N2;
-    if (!getNumber(Words1[0], N1))
-      exitWithError("invalid counter", Filename1, Num);
-    if (!getNumber(Words2[0], N2))
-      exitWithError("invalid counter", Filename2, Num);
-
-    uint64_t Sum = N1 + N2;
-    if (Sum < N1)
-      exitWithError("counter overflow", Filename2, Num);
-
-    Output << N1 + N2 << "\n";
   }
-  if (readLine(P2, End2, Line2))
-    exitWithError("truncated file", Filename1, Num + 1);
+  if (!I1.is_at_end())
+    exitWithError("truncated file", Filename1, I1.line_number());
+  if (!I2.is_at_end())
+    exitWithError("truncated file", Filename2, I2.line_number());
+  if (State != ReadName)
+    exitWithError("truncated file", Filename1, I1.line_number());
 
   return 0;
 }