Bug 1438866 - Add some utility code to help with debugging. r?emilio draft
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 16 Feb 2018 17:07:39 -0500
changeset 756876 41c880ff92f11d71ee9fd2d12c4b8ee41fccd564
parent 756875 e2ead53bceb597a5413d1a20f150c5733b06b8d5
child 756877 8ef45f9669c5e85a735396be26ab1effcfe54fd4
push id99571
push userkgupta@mozilla.com
push dateMon, 19 Feb 2018 03:19:26 +0000
reviewersemilio
bugs1438866
milestone60.0a1
Bug 1438866 - Add some utility code to help with debugging. r?emilio This adds an RAII class and macro that can be quickly added in functions to log entry/exit from the function. This is useful to debugging. MozReview-Commit-ID: 4Ud8jLOxI0R
build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp
--- a/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp
+++ b/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp
@@ -64,16 +64,30 @@ static bool isValidIdentifier(std::strin
   for (char C : Input) {
     if (!(isalpha(C) || isdigit(C) || C == '_')) {
       return false;
     }
   }
   return true;
 }
 
+struct RAIITracer {
+  RAIITracer(const char *log) : mLog(log) {
+    printf("<%s>\n", mLog);
+  }
+
+  ~RAIITracer() {
+    printf("</%s>\n", mLog);
+  }
+
+  const char* mLog;
+};
+
+#define TRACEFUNC RAIITracer tracer(__FUNCTION__);
+
 class IndexConsumer;
 
 // For each C++ file seen by the analysis (.cpp or .h), we track a
 // FileInfo. This object tracks whether the file is "interesting" (i.e., whether
 // it's in the source dir or the objdir). We also store the analysis output
 // here.
 struct FileInfo {
   FileInfo(std::string &Rname) : Realname(Rname) {
@@ -439,32 +453,40 @@ public:
       std::vector<std::string> Lines;
 
       // Read all the existing lines in from the output file. Rather than
       // overwrite them, we want to merge our results with what was already
       // there. This ensures that header files that are included multiple times
       // in different ways are analyzed completely.
       char Buffer[65536];
       FILE *Fp = Lock.openFile("r");
+      if (!Fp) {
+        fprintf(stderr, "Unable to open input file %s\n", Filename.c_str());
+        exit(1);
+      }
       while (fgets(Buffer, sizeof(Buffer), Fp)) {
         Lines.push_back(std::string(Buffer));
       }
       fclose(Fp);
 
       // Insert the newly generated analysis data into what was read. Sort the
       // results and then remove duplicates.
       Lines.insert(Lines.end(), Info.Output.begin(), Info.Output.end());
       std::sort(Lines.begin(), Lines.end());
 
       std::vector<std::string> Nodupes;
       std::unique_copy(Lines.begin(), Lines.end(), std::back_inserter(Nodupes));
 
       // Overwrite the output file with the merged data. Since we have the lock,
       // this will happen atomically.
       Fp = Lock.openFile("w");
+      if (!Fp) {
+        fprintf(stderr, "Unable to open output file %s\n", Filename.c_str());
+        exit(1);
+      }
       size_t Length = 0;
       for (std::string &Line : Nodupes) {
         Length += Line.length();
         if (fwrite(Line.c_str(), Line.length(), 1, Fp) != 1) {
           fprintf(stderr, "Unable to write to output file %s\n", Filename.c_str());
         }
       }
       fclose(Fp);