From 7df4a8b87f55fdade012c721f87c442e6ca1248d Mon Sep 17 00:00:00 2001 From: "timeless%mozdev.org" Date: Tue, 18 Nov 2003 20:52:01 +0000 Subject: [PATCH] Bug 225604 Dreftool should not follow symlinks (by default) r=dwitte --- tools/dreftool/CCPPTokenizer.cpp | 1 - tools/dreftool/CToken.cpp | 28 ---- tools/dreftool/CToken.h | 15 --- tools/dreftool/main.cpp | 212 +++++++++++++++++-------------- 4 files changed, 118 insertions(+), 138 deletions(-) diff --git a/tools/dreftool/CCPPTokenizer.cpp b/tools/dreftool/CCPPTokenizer.cpp index bb3ac66e8c7..24b6efce08e 100644 --- a/tools/dreftool/CCPPTokenizer.cpp +++ b/tools/dreftool/CCPPTokenizer.cpp @@ -105,7 +105,6 @@ int32 CCPPTokenizer::tokenize(CScanner& aScanner){ CToken* theToken=0; result=consumeToken(aScanner,&theToken); if(theToken) { - //theToken->debugDumpSource(cout); mTokens.Push(theToken); } } diff --git a/tools/dreftool/CToken.cpp b/tools/dreftool/CToken.cpp index bd07de544d1..2bffe799fbc 100644 --- a/tools/dreftool/CToken.cpp +++ b/tools/dreftool/CToken.cpp @@ -123,34 +123,6 @@ int CToken::consume(char aChar,CScanner& aScanner) { return result; } - -/** - * This debug method causes the token to dump its content - * to the given stream (formated for debugging). - * - * @update gess 3/25/98 - * @param ostream -- output stream to accept output data - */ -void CToken::debugDumpToken(ostream& anOutputStream) { - anOutputStream << "[" << getClassName() << "] "; - int i=0; - for(i=0;i<(int) mTextValue.Length();i++){ - anOutputStream << char(mTextValue.CharAt(i)); - } - anOutputStream << ": " << mTypeID << endl; -} - -/** - * This debug method causes the token to dump its content - * to the given stream, formated as text. - * - * @update gess 3/25/98 - * @param ostream -- output stream to accept output data - */ -void CToken::debugDumpSource(ostream& anOutputStream) { - anOutputStream << PromiseFlatCString(mTextValue).get() << endl; -} - /** * setter method that changes the string value of this token * @update gess5/11/98 diff --git a/tools/dreftool/CToken.h b/tools/dreftool/CToken.h index c5adc64b7fe..a45f4575a29 100644 --- a/tools/dreftool/CToken.h +++ b/tools/dreftool/CToken.h @@ -58,7 +58,6 @@ #include "SharedTypes.h" #include "nsString.h" -#include #include "CScanner.h" @@ -183,20 +182,6 @@ class CToken { */ virtual int consume(char aChar,CScanner& aScanner); - /** - * Causes token to dump itself in debug form to given output stream - * @update gess5/11/98 - * @param out is the output stream where token should write itself - */ - virtual void debugDumpToken(ostream& out); - - /** - * Causes token to dump itself in source form to given output stream - * @update gess5/11/98 - * @param out is the output stream where token should write itself - */ - virtual void debugDumpSource(ostream& out); - /** * getter which retrieves type of token * @update gess5/11/98 diff --git a/tools/dreftool/main.cpp b/tools/dreftool/main.cpp index b51d4d7f099..461afa80741 100644 --- a/tools/dreftool/main.cpp +++ b/tools/dreftool/main.cpp @@ -36,8 +36,6 @@ * * ***** END LICENSE BLOCK ***** */ -#include -#include #include "nsString.h" #include "prio.h" #include "plstr.h" @@ -47,6 +45,9 @@ #include "tokens.h" #include "CToken.h" #include "stdlib.h" +#include "nsIFile.h" +#include "nsILocalFile.h" +#include "nsISimpleEnumerator.h" //******************************************************************************* // Global variables... @@ -60,7 +61,7 @@ const char *gRootDir; class IPattern { public: - virtual CToken* scan(CCPPTokenizer& aTokenizer,int anIndex,ostream& anOutput)=0; + virtual CToken* scan(CCPPTokenizer& aTokenizer,int anIndex)=0; }; @@ -530,7 +531,7 @@ public: }//while } - void OutputMozillaFilename(ostream& anOutput) { + void OutputMozillaFilename() { mFilename.ReplaceChar('\\','/'); PRInt32 pos=mFilename.RFind("/") + 1; nsCAutoString entries,repository; @@ -549,21 +550,26 @@ public: bool found = false; { fstream cvsrecord (entries.get(),ios::in); - while (!cvsrecord.eof()) { - cvsrecord.get(prefix); - if (prefix=='/') { - cvsrecord.getline(cvsfilename,sizeof cvsfilename, '/'); - if (found = !strcmp(filename.get(), cvsfilename)) - break; + if (cvsrecord.is_open()) { + while (!cvsrecord.eof()) { + cvsrecord.get(prefix); + if (prefix=='/') { + cvsrecord.getline(cvsfilename,sizeof cvsfilename, '/'); + if (found = !strcmp(filename.get(), cvsfilename)) + break; + } + cvsrecord.ignore(256,'\n'); } - cvsrecord.ignore(256,'\n'); + if (found) + cvsrecord.getline(version,sizeof version, '/'); } - if (found) - cvsrecord.getline(version,sizeof version, '/'); } { fstream cvsrecord (repository.get(),ios::in); - cvsrecord.getline(cvspath,sizeof cvspath); + if (cvsrecord.is_open()) + cvsrecord.getline(cvspath,sizeof cvspath); + else + cvspath[0]=0; if (cvspath[0]) filename = NS_LITERAL_CSTRING("/") + @@ -579,37 +585,36 @@ public: if(-1"; - } - - cout << buf << ":" << mLineNumber; - - if(gEmitHTML) { - cout << ""; - } - + if(gEmitHTML) + fprintf(stdout, "
%s:%d", + buf, + mLineNumber); } else { - anOutput << buf; + fprintf(stdout, "%s", buf); } } @@ -622,7 +627,7 @@ public: When we find one, we look it up in mSafeNames. If it's not there, we have a potential deref error. ***************************************************************************/ - void scan(CCPPTokenizer& aTokenizer,ostream& anOutput){ + void scan(CCPPTokenizer& aTokenizer){ int theMax=aTokenizer.getCount(); int theIDCount=mNames.GetSize(); int theSafeIDCount=mSafeNames.GetSize(); @@ -639,7 +644,7 @@ public: switch(theChar){ case '{': mIndex++; - scan(aTokenizer,anOutput); + scan(aTokenizer); break; case '}': //before returning, let's remove the new identifiers... @@ -718,11 +723,11 @@ public: } //dump the name out in LXR format - OutputMozillaFilename(anOutput); + OutputMozillaFilename(); - anOutput << " Deref-error: \"" - << PromiseFlatCString(theString).get() - << "\"" << endl; + fprintf(stdout, + " Deref-error: \"%s\"\n", + theString.get()); mErrCount++; } } @@ -744,45 +749,51 @@ public: nsCString mFilename; }; -void ScanFile(const nsAFlatCString& aFilename,ostream& anOutput,int& aLineCount,int& anErrCount) { - nsCAutoString theCStr(aFilename); - - fstream input(PromiseFlatCString(theCStr).get(),ios::in); +void ScanFile(nsIFile *aFilename,int& aLineCount,int& anErrCount) { + nsCAutoString theCStr; + if (NS_FAILED(aFilename->GetNativePath(theCStr)) || theCStr.IsEmpty()) + return; + fstream input(theCStr.get(),ios::in); CScanner theScanner(input); CCPPTokenizer theTokenizer; theTokenizer.tokenize(theScanner); - CPattern thePattern(aFilename); - thePattern.scan(theTokenizer,anOutput); + CPattern thePattern(theCStr); + thePattern.scan(theTokenizer); aLineCount+=thePattern.mLineNumber; anErrCount+=thePattern.mErrCount; } -void IteratePath(const char *aPath, ostream& anOutput, - int& aFilecount,int& aLinecount,int& anErrcount) { - PRFileInfo eInfo; - if (PR_SUCCESS == PR_GetFileInfo(aPath, &eInfo)) { - if (eInfo.type == PR_FILE_DIRECTORY) { - if (PRDir *aDir = PR_OpenDir(aPath)) { - while (PRDirEntry *anEntry = PR_ReadDir(aDir, PR_SKIP_BOTH)) { - nsCAutoString path( - nsDependentCString(aPath) + - NS_LITERAL_CSTRING("/") + - nsDependentCString(anEntry->name) - ); - if (0 != PL_strcasecmp(anEntry->name, "CVS")) - IteratePath(path.get(), anOutput, aFilecount, aLinecount, anErrcount); +void IteratePath(nsIFile *aPath, + int& aFilecount,int& aLinecount,int& anErrcount) +{ + PRBool is = PR_FALSE; + nsXPIDLCString name; + if (NS_FAILED(aPath->GetNativeLeafName(name)) || + name.IsEmpty()) + return; + if (NS_FAILED(aPath->IsSymlink(&is)) || is || + NS_FAILED(aPath->IsSpecial(&is)) || is) { + fprintf(stderr, "Skipping: %s\n", name.get()); + } else if (NS_SUCCEEDED(aPath->IsDirectory(&is)) && is && + !name.Equals(NS_LITERAL_CSTRING("CVS"))) { + nsCOMPtr dirList; + if (NS_SUCCEEDED(aPath->GetDirectoryEntries(getter_AddRefs(dirList)))) { + while (NS_SUCCEEDED(dirList->HasMoreElements(&is)) && is) { + nsCOMPtr thing; + nsCOMPtr path; + if (NS_SUCCEEDED(dirList->GetNext(getter_AddRefs(thing))) && + (path = do_QueryInterface(thing))) { + IteratePath(path, aFilecount, aLinecount, anErrcount); } } - } - else if (eInfo.type == PR_FILE_FILE) { - const char *ext = PL_strcaserstr(aPath, ".cpp"); - if (ext && (ext[4] == 0)) { - aFilecount++; - ScanFile(nsDependentCString(aPath),anOutput,aLinecount,anErrcount); - } - } + } + } else if (NS_SUCCEEDED(aPath->IsFile(&is)) && is && + (StringEndsWith(name, NS_LITERAL_CSTRING(".cpp")) || + StringEndsWith(name, NS_LITERAL_CSTRING(".c")))) { + aFilecount++; + ScanFile(aPath,aLinecount,anErrcount); } } @@ -810,10 +821,11 @@ void ConsumeArguments(int argc, char* argv[]) { break; default: - cout << "Usage: dreftool [-h] [-d sourcepath]" << endl; - cout << "-d path to root of source tree" << endl; - cout << "-s sloppy mode no warnings if member variables are used unsafe" <" << endl; + fprintf(stdout, "\n"); } - IteratePath(gRootDir,cout,fileCount,lineCount,errCount); + { + nsCOMPtr root; + nsCOMPtr localroot; + if (NS_SUCCEEDED(NS_NewNativeLocalFile(nsDependentCString(gRootDir), PR_TRUE, getter_AddRefs(localroot))) && + (root = do_QueryInterface(localroot)) && + NS_SUCCEEDED(root->Normalize())) + IteratePath(root,fileCount,lineCount,errCount); + else + fprintf(stderr, "Could not find path: %s\n", gRootDir); + } #endif if(gEmitHTML) { - cout << "
" <\n");
   }
 
-  cout << endl << "Summary";
+  fprintf(stdout, "\nSummary");
   if (gRootDir[0]!='.' || gRootDir[1])
-    cout << " for " << gRootDir;
-  cout << ": " << endl;
-  cout << "===============================" << endl;
-  cout<< "Files:  " << fileCount << endl;
-  cout<< "Lines:  " << lineCount<< endl;
-  cout<< "Errors: " << errCount<< endl;
+    fprintf(stdout, " for %s", gRootDir);
+  fprintf(stdout,
+          ": \n"
+          "===============================\n"
+          "Files:  %d\n"
+          "Lines:  %d\n"
+          "Errors: %d\n",
+          fileCount,
+          lineCount,
+          errCount);
 
   if(gEmitHTML) {
-    cout << "
" <" << endl; + fprintf(stdout, "\n\n"); }