Guard against most unintentionally ephemeral SkAutoFoo instantiations.

I think I applied the trick everywhere possible.  Limitations:
    - can't be used with templated classes
    - all constructors and destructors must be defined inline

A couple of the SkAutoFoo were unused in Skia, Chromium, and Android, so I
deleted them.  This change caught the same bugs Cary found in SkPath, plus one
more in SampleApp.

BUG=
R=reed@google.com, caryclark@google.com

Author: mtklein@google.com

Review URL: https://codereview.chromium.org/72603005

git-svn-id: http://skia.googlecode.com/svn/trunk/include@12301 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/core/SkBitmap.h b/core/SkBitmap.h
index dd2d2cf..d9749f0 100644
--- a/core/SkBitmap.h
+++ b/core/SkBitmap.h
@@ -730,6 +730,7 @@
     const SkBitmap& fBitmap;
     bool            fDidLock;
 };
+#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
 
 /** Helper class that performs the lock/unlockColors calls on a colortable.
     The destructor will call unlockColors(false) if it has a bitmap's colortable
@@ -783,6 +784,7 @@
     SkColorTable*    fCTable;
     const SkPMColor* fColors;
 };
+#define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors)
 
 ///////////////////////////////////////////////////////////////////////////////
 
diff --git a/core/SkCanvas.h b/core/SkCanvas.h
index c963edc..d831a5c 100644
--- a/core/SkCanvas.h
+++ b/core/SkCanvas.h
@@ -1158,6 +1158,7 @@
     SkCanvas*   fCanvas;
     int         fSaveCount;
 };
+#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
 
 /** Stack helper class to automatically open and close a comment block
  */
@@ -1179,5 +1180,6 @@
 private:
     SkCanvas* fCanvas;
 };
+#define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock)
 
 #endif
diff --git a/core/SkMask.h b/core/SkMask.h
index 7155184..5cfef97 100644
--- a/core/SkMask.h
+++ b/core/SkMask.h
@@ -157,5 +157,6 @@
 private:
     uint8_t* fImage;
 };
+#define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage)
 
 #endif
diff --git a/core/SkPicture.h b/core/SkPicture.h
index fa1cc49..e371c43 100644
--- a/core/SkPicture.h
+++ b/core/SkPicture.h
@@ -253,26 +253,6 @@
     typedef SkRefCnt INHERITED;
 };
 
-class SkAutoPictureRecord : SkNoncopyable {
-public:
-    SkAutoPictureRecord(SkPicture* pict, int width, int height,
-                        uint32_t recordingFlags = 0) {
-        fPicture = pict;
-        fCanvas = pict->beginRecording(width, height, recordingFlags);
-    }
-    ~SkAutoPictureRecord() {
-        fPicture->endRecording();
-    }
-
-    /** Return the canvas to draw into for recording into the picture.
-    */
-    SkCanvas* getRecordingCanvas() const { return fCanvas; }
-
-private:
-    SkPicture*  fPicture;
-    SkCanvas*   fCanvas;
-};
-
 /**
  *  Subclasses of this can be passed to canvas.drawPicture. During the drawing
  *  of the picture, this callback will periodically be invoked. If its
diff --git a/core/SkRefCnt.h b/core/SkRefCnt.h
index 99df0c1..b010faf 100644
--- a/core/SkRefCnt.h
+++ b/core/SkRefCnt.h
@@ -237,11 +237,13 @@
 private:
     T*  fObj;
 };
+// Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :(
 
 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
 public:
     SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
 };
+#define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref)
 
 class SkAutoRef : SkNoncopyable {
 public:
@@ -250,6 +252,7 @@
 private:
     SkRefCnt* fObj;
 };
+#define SkAutoRef(...) SK_REQUIRE_LOCAL_VAR(SkAutoRef)
 
 /** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
     a SkRefCnt (or subclass) object.
diff --git a/core/SkString.h b/core/SkString.h
index 5d97533..5629cc0 100644
--- a/core/SkString.h
+++ b/core/SkString.h
@@ -235,24 +235,6 @@
     static Rec* RefRec(Rec*);
 };
 
-class SkAutoUCS2 {
-public:
-    SkAutoUCS2(const char utf8[]);
-    ~SkAutoUCS2();
-
-    /** This returns the number of ucs2 characters
-    */
-    int count() const { return fCount; }
-
-    /** This returns a null terminated ucs2 string
-    */
-    const uint16_t* getUCS2() const { return fUCS2; }
-
-private:
-    int         fCount;
-    uint16_t*   fUCS2;
-};
-
 /// Creates a new string and writes into it using a printf()-style format.
 SkString SkStringPrintf(const char* format, ...);
 
diff --git a/core/SkThread.h b/core/SkThread.h
index 4a2499a..487c2bd 100644
--- a/core/SkThread.h
+++ b/core/SkThread.h
@@ -64,5 +64,6 @@
 private:
     SkBaseMutex* fMutex;
 };
+#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
 
 #endif
diff --git a/core/SkTime.h b/core/SkTime.h
index 7f3c270..51616d4 100644
--- a/core/SkTime.h
+++ b/core/SkTime.h
@@ -60,5 +60,6 @@
     SkMSec      fNow;
     SkMSec      fMinToDump;
 };
+#define SkAutoTime(...) SK_REQUIRE_LOCAL_VAR(SkAutoTime)
 
 #endif
diff --git a/core/SkTypes.h b/core/SkTypes.h
index 219e51f..c30a8a2 100644
--- a/core/SkTypes.h
+++ b/core/SkTypes.h
@@ -149,6 +149,32 @@
  */
 #define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
 
+/**
+ * For some classes, it's almost always an error to instantiate one without a name, e.g.
+ *   {
+ *       SkAutoMutexAcquire(&mutex);
+ *       <some code>
+ *   }
+ * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
+ * but instead the mutex is acquired and then immediately released.  The correct usage is
+ *   {
+ *       SkAutoMutexAcquire lock(&mutex);
+ *       <some code>
+ *   }
+ *
+ * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
+ * like this:
+ *   class classname {
+ *       <your class>
+ *   };
+ *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
+ *
+ * This won't work with templates, and you must inline the class' constructors and destructors.
+ * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
+ */
+#define SK_REQUIRE_LOCAL_VAR(classname) \
+    SK_COMPILE_ASSERT(false, missing_name_for_##classname)
+
 ///////////////////////////////////////////////////////////////////////
 
 /**
@@ -432,6 +458,7 @@
     SkAutoFree(const SkAutoFree&);
     SkAutoFree& operator=(const SkAutoFree&);
 };
+#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
 
 /**
  *  Manage an allocated block of heap memory. This object is the sole manager of
@@ -518,6 +545,7 @@
     void*   fPtr;
     size_t  fSize;  // can be larger than the requested size (see kReuse)
 };
+#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
 
 /**
  *  Manage an allocated block of memory. If the requested size is <= kSize, then
@@ -604,6 +632,7 @@
     size_t      fSize;  // can be larger than the requested size (see kReuse)
     uint32_t    fStorage[(kSize + 3) >> 2];
 };
+// Can't guard the constructor because it's a template class.
 
 #endif /* C++ */
 
diff --git a/core/SkUtils.h b/core/SkUtils.h
index e29367d..c2dcad9 100644
--- a/core/SkUtils.h
+++ b/core/SkUtils.h
@@ -113,5 +113,6 @@
 private:
     const char* fLabel;
 };
+#define SkAutoTrace(...) SK_REQUIRE_LOCAL_VAR(SkAutoTrace)
 
 #endif