[cfe-commits] r39704 - in /cfe/cfe/trunk/Driver: DiagChecker.cpp DiagChecker.h clang.cpp clang.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:47:20 PDT 2007


Author: clattner
Date: Wed Jul 11 11:47:20 2007
New Revision: 39704

URL: http://llvm.org/viewvc/llvm-project?rev=39704&view=rev
Log:
Eliminate almost all of the redundancy Bill introduced.

Removed:
    cfe/cfe/trunk/Driver/DiagChecker.h
Modified:
    cfe/cfe/trunk/Driver/DiagChecker.cpp
    cfe/cfe/trunk/Driver/clang.cpp
    cfe/cfe/trunk/Driver/clang.h

Modified: cfe/cfe/trunk/Driver/DiagChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/DiagChecker.cpp?rev=39704&r1=39703&r2=39704&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/DiagChecker.cpp (original)
+++ cfe/cfe/trunk/Driver/DiagChecker.cpp Wed Jul 11 11:47:20 2007
@@ -11,8 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "DiagChecker.h"
+#include "clang.h"
 #include "ASTStreamers.h"
+#include "TextDiagnosticBuffer.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Preprocessor.h"
 using namespace clang;
@@ -41,7 +42,7 @@
 /// diagnostics. If so, then put them in a diagnostic list.
 /// 
 static void FindDiagnostics(const std::string &Comment,
-                            TextDiagnosticBuffer &DiagClient,
+                            const TextDiagnosticBuffer &DiagClient,
                             DiagList &ExpectedDiags,
                             SourceManager &SourceMgr,
                             SourceLocation Pos,
@@ -85,19 +86,18 @@
 /// called later to report any discrepencies between the diagnostics expected
 /// and those actually seen.
 /// 
-void clang::ProcessFileDiagnosticChecking(TextDiagnosticBuffer &DiagClient,
+static void ProcessFileDiagnosticChecking(const TextDiagnosticBuffer&DiagClient,
                                           Preprocessor &PP,
-                                          const std::string &InFile,
-                                          SourceManager &SourceMgr,
                                           unsigned MainFileID,
                                           DiagList &ExpectedErrors,
                                           DiagList &ExpectedWarnings) {
-  LexerToken Tok;
+  // Return comments as tokens, this is how we find expected diagnostics.
   PP.SetCommentRetentionState(true, true);
 
   // Enter the cave.
   PP.EnterSourceFile(MainFileID, 0, true);
 
+  LexerToken Tok;
   do {
     PP.Lex(Tok);
 
@@ -105,12 +105,12 @@
       std::string Comment = PP.getSpelling(Tok);
 
       // Find all expected errors
-      FindDiagnostics(Comment, DiagClient, ExpectedErrors, SourceMgr,
+      FindDiagnostics(Comment, DiagClient, ExpectedErrors,PP.getSourceManager(),
                       Tok.getLocation(), ExpectedErrStr);
 
       // Find all expected warnings
-      FindDiagnostics(Comment, DiagClient, ExpectedWarnings, SourceMgr,
-                      Tok.getLocation(), ExpectedWarnStr);
+      FindDiagnostics(Comment, DiagClient, ExpectedWarnings,
+                      PP.getSourceManager(),Tok.getLocation(), ExpectedWarnStr);
     }
   } while (Tok.getKind() != tok::eof);
 
@@ -181,7 +181,7 @@
 /// were actually reported. It emits any discrepencies. Return "true" if there
 /// were problems. Return "false" otherwise.
 /// 
-bool clang::ReportCheckingResults(TextDiagnosticBuffer &DiagClient,
+static bool ReportCheckingResults(const TextDiagnosticBuffer &DiagClient,
                                   const DiagList &ExpectedErrors,
                                   const DiagList &ExpectedWarnings,
                                   SourceManager &SourceMgr) {
@@ -220,3 +220,20 @@
 
   return HadProblem;
 }
+
+/// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
+bool clang::CheckDiagnostics(Preprocessor &PP, unsigned MainFileID) {
+  const TextDiagnosticBuffer &Diags =
+    static_cast<const TextDiagnosticBuffer&>(PP.getDiagnostics().getClient());
+
+  // Gather the set of expected diagnostics.
+  DiagList ExpectedErrors, ExpectedWarnings;
+  ProcessFileDiagnosticChecking(Diags, PP, MainFileID, ExpectedErrors,
+                                ExpectedWarnings);
+    
+  
+  return ReportCheckingResults(Diags, ExpectedErrors,
+                               ExpectedWarnings, PP.getSourceManager());
+}
+
+

Removed: cfe/cfe/trunk/Driver/DiagChecker.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/DiagChecker.h?rev=39703&view=auto

==============================================================================
--- cfe/cfe/trunk/Driver/DiagChecker.h (original)
+++ cfe/cfe/trunk/Driver/DiagChecker.h (removed)
@@ -1,43 +0,0 @@
-//===--- DiagChecker.h - Diagnostic Checking Functions ----------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file was developed by Bill Wendling and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// Check diagnostic messages.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef DRIVER_DIAG_CHECKER_H_
-#define DRIVER_DIAG_CHECKER_H_
-
-#include "TextDiagnosticBuffer.h"
-#include <string>
-
-namespace clang {
-
-class Preprocessor;
-class SourceLocation;
-class SourceManager;
-
-void ProcessFileDiagnosticChecking(TextDiagnosticBuffer &DiagClient,
-                                   Preprocessor &PP, const std::string &InFile,
-                                   SourceManager &SourceMgr,
-                                   unsigned MainFileID,
-                                   TextDiagnosticBuffer::DiagList
-                                     &ExpectedErrors,
-                                   TextDiagnosticBuffer::DiagList
-                                     &ExpectedWarnings);
-bool ReportCheckingResults(TextDiagnosticBuffer &DiagClient,
-                           const TextDiagnosticBuffer::DiagList
-                             &ExpectedErrors,
-                           const TextDiagnosticBuffer::DiagList
-                             &ExpectedWarnings,
-                           SourceManager &SourceMgr);
-
-} // end clang namespace
-
-#endif

Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=39704&r1=39703&r2=39704&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:47:20 2007
@@ -24,7 +24,7 @@
 
 #include "clang.h"
 #include "ASTStreamers.h"
-#include "DiagChecker.h"
+#include "TextDiagnosticBuffer.h"
 #include "TextDiagnosticPrinter.h"
 #include "clang/Parse/Parser.h"
 #include "clang/Lex/HeaderSearch.h"
@@ -34,6 +34,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Signals.h"
+#include <memory>
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -805,6 +806,9 @@
   case EmitLLVM:
     EmitLLVMFromASTs(PP, MainFileID, Stats);
     break;
+  case ParseASTCheck:
+    exit(CheckDiagnostics(PP, MainFileID));
+    break;
   }
   
   if (Stats) {
@@ -819,74 +823,46 @@
 static llvm::cl::list<std::string>
 InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
 
-/// PerformNormalFileProcessing - This processes each file in turn, printing out
-/// the diagnostic messages to stderr. It returns a pair: number of diagnostic
-/// messages, number of errors.
-/// 
-static std::pair<unsigned, unsigned>
-PerformNormalFileProcessing(SourceManager &SourceMgr,
-                            FileManager &FileMgr,
-                            LangOptions &LangInfo) {
-  // Print diagnostics to stderr.
-  TextDiagnosticPrinter OurDiagnosticClient(SourceMgr);
+
+int main(int argc, char **argv) {
+  llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
+  llvm::sys::PrintStackTraceOnErrorSignal();
   
-  // Configure our handling of diagnostics.
-  Diagnostic Diags(OurDiagnosticClient);
-  InitializeDiagnostics(Diags);
+  // If no input was specified, read from stdin.
+  if (InputFilenames.empty())
+    InputFilenames.push_back("-");
   
-  // Get information about the targets being compiled for.  Note that this
-  // pointer and the TargetInfoImpl objects are never deleted by this toy
-  // driver.
-  TargetInfo *Target = CreateTargetInfo(Diags);
-  if (Target == 0) {
-    fprintf(stderr,
-            "Sorry, don't know what target this is, please use -arch.\n");
-    exit(1);
-  }
+  /// Create a SourceManager object.  This tracks and owns all the file buffers
+  /// allocated to the program.
+  SourceManager SourceMgr;
   
-  // Process the -I options and set them in the HeaderInfo.
-  HeaderSearch HeaderInfo(FileMgr);
-  OurDiagnosticClient.setHeaderSearch(HeaderInfo);
-  InitializeIncludePaths(HeaderInfo, FileMgr, Diags, LangInfo);
+  // Create a file manager object to provide access to and cache the filesystem.
+  FileManager FileMgr;
   
-  for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
-    // Set up the preprocessor with these options.
-    Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
-    OurDiagnosticClient.setPreprocessor(PP);
-    const std::string &InFile = InputFilenames[i];
-    std::vector<char> PrologMacros;
-    unsigned MainFileID = InitializePreprocessor(PP, InFile, SourceMgr,
-                                                 HeaderInfo, LangInfo,
-                                                 PrologMacros);
-
-    if (!MainFileID) continue;
+  // Initialize language options, inferring file types from input filenames.
+  // FIXME: This infers info from the first file, we should clump by language
+  // to handle 'x.c y.c a.cpp b.cpp'.
+  LangOptions LangInfo;
+  InitializeBaseLanguage(LangInfo, InputFilenames[0]);
+  InitializeLanguageStandard(LangInfo);
 
-    ProcessInputFile(PP, MainFileID, InFile, SourceMgr,
-                     OurDiagnosticClient, HeaderInfo, LangInfo);
-    HeaderInfo.ClearFileInfo();
+  std::auto_ptr<TextDiagnostics> DiagClient;
+  if (ProgAction != ParseASTCheck) {
+    // Print diagnostics to stderr by default.
+    DiagClient.reset(new TextDiagnosticPrinter(SourceMgr));
+  } else {
+    // When checking diagnostics, just buffer them up.
+    DiagClient.reset(new TextDiagnosticBuffer(SourceMgr));
+   
+    if (InputFilenames.size() != 1) {
+      fprintf(stderr,
+              "parse-ast-check only works on single input files for now.\n");
+      return 1;
+    }
   }
   
-  unsigned NumDiagnostics = Diags.getNumDiagnostics();
-
-  if (NumDiagnostics)
-    fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
-            (NumDiagnostics == 1 ? "" : "s"));
-
-  return std::make_pair(Diags.getNumDiagnostics(), Diags.getNumErrors());
-}
-
-/// PerformDiagnosticChecking - This processes each file in turn, checking that
-/// each diagnostic message is expect.
-/// 
-static std::pair<unsigned, unsigned>
-PerformDiagnosticChecking(SourceManager &SourceMgr,
-                          FileManager &FileMgr,
-                          LangOptions &LangInfo) {
-  // Check diagnostic messages.
-  TextDiagnosticBuffer OurDiagnosticClient(SourceMgr);
-  
   // Configure our handling of diagnostics.
-  Diagnostic Diags(OurDiagnosticClient);
+  Diagnostic Diags(*DiagClient);
   InitializeDiagnostics(Diags);
   
   // Get information about the targets being compiled for.  Note that this
@@ -901,67 +877,31 @@
   
   // Process the -I options and set them in the HeaderInfo.
   HeaderSearch HeaderInfo(FileMgr);
-  OurDiagnosticClient.setHeaderSearch(HeaderInfo);
+  DiagClient->setHeaderSearch(HeaderInfo);
   InitializeIncludePaths(HeaderInfo, FileMgr, Diags, LangInfo);
-
-  TextDiagnosticBuffer::DiagList ExpectedErrors;
-  TextDiagnosticBuffer::DiagList ExpectedWarnings;
   
   for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
     // Set up the preprocessor with these options.
     Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
-    OurDiagnosticClient.setPreprocessor(PP);
+    DiagClient->setPreprocessor(PP);
     const std::string &InFile = InputFilenames[i];
     std::vector<char> PrologMacros;
     unsigned MainFileID = InitializePreprocessor(PP, InFile, SourceMgr,
                                                  HeaderInfo, LangInfo,
                                                  PrologMacros);
-
+    
     if (!MainFileID) continue;
 
-    ProcessFileDiagnosticChecking(OurDiagnosticClient, PP, InFile,
-                                  SourceMgr, MainFileID, ExpectedErrors,
-                                  ExpectedWarnings);
+    ProcessInputFile(PP, MainFileID, InFile, SourceMgr,
+                     *DiagClient, HeaderInfo, LangInfo);
     HeaderInfo.ClearFileInfo();
   }
-
-  if (ReportCheckingResults(OurDiagnosticClient, ExpectedErrors,
-                            ExpectedWarnings, SourceMgr))
-    // There were errors. Fake it.
-    return std::make_pair(1, 1);
-
-  return std::make_pair(0, 0);
-}
-
-int main(int argc, char **argv) {
-  llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
-  llvm::sys::PrintStackTraceOnErrorSignal();
   
-  // If no input was specified, read from stdin.
-  if (InputFilenames.empty())
-    InputFilenames.push_back("-");
-  
-  /// Create a SourceManager object.  This tracks and owns all the file buffers
-  /// allocated to the program.
-  SourceManager SourceMgr;
-  
-  // Create a file manager object to provide access to and cache the filesystem.
-  FileManager FileMgr;
+  unsigned NumDiagnostics = Diags.getNumDiagnostics();
   
-  // Initialize language options, inferring file types from input filenames.
-  // FIXME: This infers info from the first file, we should clump by language
-  // to handle 'x.c y.c a.cpp b.cpp'.
-  LangOptions LangInfo;
-  InitializeBaseLanguage(LangInfo, InputFilenames[0]);
-  InitializeLanguageStandard(LangInfo);
-
-  // Pair to represent the number of diagnostics and number of errors emitted.
-  std::pair<unsigned, unsigned> NumDiagsOutput = std::make_pair(0, 0);
-
-  if (ProgAction != ParseASTCheck)
-    NumDiagsOutput = PerformNormalFileProcessing(SourceMgr, FileMgr, LangInfo);
-  else
-    NumDiagsOutput = PerformDiagnosticChecking(SourceMgr, FileMgr, LangInfo);
+  if (NumDiagnostics)
+    fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
+            (NumDiagnostics == 1 ? "" : "s"));
   
   if (Stats) {
     // Printed from high-to-low level.
@@ -970,5 +910,5 @@
     fprintf(stderr, "\n");
   }
   
-  return NumDiagsOutput.second != 0;
+  return Diags.getNumErrors();
 }

Modified: cfe/cfe/trunk/Driver/clang.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.h?rev=39704&r1=39703&r2=39704&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.h (original)
+++ cfe/cfe/trunk/Driver/clang.h Wed Jul 11 11:47:20 2007
@@ -33,10 +33,13 @@
 /// the -arch command line option.
 TargetInfo *CreateTargetInfo(Diagnostic &Diags);
 
-
+/// EmitLLVMFromASTs - Implement -emit-llvm, which generates llvm IR from C.
 void EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
                       bool PrintStats);
   
+/// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
+bool CheckDiagnostics(Preprocessor &PP, unsigned MainFileID);
+
 }  // end namespace clang
 
 #endif





More information about the cfe-commits mailing list