[analyzer] Testing: add automated reference results reset.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149682 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/analyzer/SATestBuild.py b/utils/analyzer/SATestBuild.py
index e76bb21..c2649a3 100755
--- a/utils/analyzer/SATestBuild.py
+++ b/utils/analyzer/SATestBuild.py
@@ -368,10 +368,36 @@
     print "Diagnostic comparison complete (time: %.2f)." % (time.time()-TBegin) 
     return (NumDiffs > 0)
     
-def testProject(ID, InIsReferenceBuild, IsScanBuild , Dir=None):
-    global IsReferenceBuild
-    IsReferenceBuild = InIsReferenceBuild
+def updateSVN(Mode, ProjectsMap):
+    try:
+        ProjectsMap.seek(0)    
+        for I in csv.reader(ProjectsMap):
+            ProjName = I[0] 
+            Path = os.path.join(ProjName, getSBOutputDirName())
+    
+            if Mode == "delete":
+                Command = "svn delete %s" % (Path,)
+            else:
+                Command = "svn add %s" % (Path,)
 
+            if Verbose == 1:        
+                print "  Executing: %s" % (Command,)
+                check_call(Command, shell=True)    
+    
+        if Mode == "delete":
+            CommitCommand = "svn commit -m \"[analyzer tests] Remove " \
+                            "reference results.\""     
+        else:
+            CommitCommand = "svn commit -m \"[analyzer tests] Add new " \
+                            "reference results.\""
+        if Verbose == 1:        
+            print "  Executing: %s" % (CommitCommand,)
+            check_call(CommitCommand, shell=True)    
+    except:
+        print "Error: SVN update failed."
+        sys.exit(-1)
+        
+def testProject(ID, IsScanBuild, Dir=None):
     print " \n\n--- Building project %s" % (ID,)
 
     TBegin = time.time() 
@@ -382,8 +408,9 @@
         print "  Build directory: %s." % (Dir,)
     
     # Set the build results directory.
-    SBOutputDir = os.path.join(Dir, getSBOutputDirName())
-        
+    RelOutputDir = getSBOutputDirName()
+    SBOutputDir = os.path.join(Dir, RelOutputDir)
+                
     buildProject(Dir, SBOutputDir, IsScanBuild)    
 
     checkBuild(SBOutputDir)
@@ -394,19 +421,36 @@
     print "Completed tests for project %s (time: %.2f)." % \
           (ID, (time.time()-TBegin))
     
-def testAll(InIsReferenceBuild = False):
-    
+def testAll(InIsReferenceBuild = False, UpdateSVN = False):
+    global IsReferenceBuild
+    IsReferenceBuild = InIsReferenceBuild
+
     PMapFile = open(getProjectMapPath(), "rb")
-    try:
-        PMapReader = csv.reader(PMapFile)
-        for I in PMapReader:
+    try:        
+        # Validate the input.
+        for I in csv.reader(PMapFile):
             if (len(I) != 2) :
                 print "Error: Rows in the ProjectMapFile should have 3 entries."
                 raise Exception()
             if (not ((I[1] == "1") | (I[1] == "0"))):
                 print "Error: Second entry in the ProjectMapFile should be 0 or 1."
                 raise Exception()              
-            testProject(I[0], InIsReferenceBuild, int(I[1]))
+
+        # When we are regenerating the reference results, we might need to 
+        # update svn. Remove reference results from SVN.
+        if UpdateSVN == True:
+            assert(InIsReferenceBuild == True);
+            updateSVN("delete",  PMapFile);
+            
+        # Test the projects.
+        PMapFile.seek(0)    
+        for I in csv.reader(PMapFile):
+            testProject(I[0], int(I[1]))
+
+        # Add reference results to SVN.
+        if UpdateSVN == True:
+            updateSVN("add",  PMapFile);
+
     except:
         print "Error occurred. Premature termination."
         raise                            
@@ -414,4 +458,18 @@
         PMapFile.close()    
             
 if __name__ == '__main__':
-    testAll()
+    IsReference = False
+    UpdateSVN = False
+    if len(sys.argv) >= 2:
+        if sys.argv[1] == "-r":
+            IsReference = True
+        elif sys.argv[1] == "-rs":
+            IsReference = True
+            UpdateSVN = True
+        else:     
+          print >> sys.stderr, 'Usage: ', sys.argv[0],\
+                             '[-r|-rs]' \
+                             'Use -r to regenerate reference output' \
+                             'Use -rs to regenerate reference output and update svn'
+
+    testAll(IsReference, UpdateSVN)