clang API Documentation

Internals.h
Go to the documentation of this file.
00001 //===-- Internals.h - Implementation Details---------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
00011 #define LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
00012 
00013 #include "clang/ARCMigrate/ARCMT.h"
00014 #include "llvm/ADT/ArrayRef.h"
00015 
00016 namespace clang {
00017   class Sema;
00018   class Stmt;
00019 
00020 namespace arcmt {
00021 
00022 class CapturedDiagList {
00023   typedef std::list<StoredDiagnostic> ListTy;
00024   ListTy List;
00025   
00026 public:
00027   void push_back(const StoredDiagnostic &diag) { List.push_back(diag); }
00028 
00029   bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
00030   bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
00031 
00032   void reportDiagnostics(DiagnosticsEngine &diags) const;
00033 
00034   bool hasErrors() const;
00035 
00036   typedef ListTy::const_iterator iterator;
00037   iterator begin() const { return List.begin(); }
00038   iterator end()   const { return List.end();   }
00039 };
00040 
00041 void writeARCDiagsToPlist(const std::string &outPath,
00042                           ArrayRef<StoredDiagnostic> diags,
00043                           SourceManager &SM, const LangOptions &LangOpts);
00044 
00045 class TransformActions {
00046   DiagnosticsEngine &Diags;
00047   CapturedDiagList &CapturedDiags;
00048   bool ReportedErrors;
00049   void *Impl; // TransformActionsImpl.
00050 
00051 public:
00052   TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
00053                    ASTContext &ctx, Preprocessor &PP);
00054   ~TransformActions();
00055 
00056   void startTransaction();
00057   bool commitTransaction();
00058   void abortTransaction();
00059 
00060   void insert(SourceLocation loc, StringRef text);
00061   void insertAfterToken(SourceLocation loc, StringRef text);
00062   void remove(SourceRange range);
00063   void removeStmt(Stmt *S);
00064   void replace(SourceRange range, StringRef text);
00065   void replace(SourceRange range, SourceRange replacementRange);
00066   void replaceStmt(Stmt *S, StringRef text);
00067   void replaceText(SourceLocation loc, StringRef text,
00068                    StringRef replacementText);
00069   void increaseIndentation(SourceRange range,
00070                            SourceLocation parentIndent);
00071 
00072   bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
00073   bool clearAllDiagnostics(SourceRange range) {
00074     return clearDiagnostic(ArrayRef<unsigned>(), range);
00075   }
00076   bool clearDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
00077     unsigned IDs[] = { ID1, ID2 };
00078     return clearDiagnostic(IDs, range);
00079   }
00080   bool clearDiagnostic(unsigned ID1, unsigned ID2, unsigned ID3,
00081                        SourceRange range) {
00082     unsigned IDs[] = { ID1, ID2, ID3 };
00083     return clearDiagnostic(IDs, range);
00084   }
00085 
00086   bool hasDiagnostic(unsigned ID, SourceRange range) {
00087     return CapturedDiags.hasDiagnostic(ID, range);
00088   }
00089 
00090   bool hasDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
00091     unsigned IDs[] = { ID1, ID2 };
00092     return CapturedDiags.hasDiagnostic(IDs, range);
00093   }
00094 
00095   void reportError(StringRef error, SourceLocation loc,
00096                    SourceRange range = SourceRange());
00097   void reportWarning(StringRef warning, SourceLocation loc,
00098                    SourceRange range = SourceRange());
00099   void reportNote(StringRef note, SourceLocation loc,
00100                   SourceRange range = SourceRange());
00101 
00102   bool hasReportedErrors() const { return ReportedErrors; }
00103 
00104   class RewriteReceiver {
00105   public:
00106     virtual ~RewriteReceiver();
00107 
00108     virtual void insert(SourceLocation loc, StringRef text) = 0;
00109     virtual void remove(CharSourceRange range) = 0;
00110     virtual void increaseIndentation(CharSourceRange range,
00111                                      SourceLocation parentIndent) = 0;
00112   };
00113 
00114   void applyRewrites(RewriteReceiver &receiver);
00115 };
00116 
00117 class Transaction {
00118   TransformActions &TA;
00119   bool Aborted;
00120 
00121 public:
00122   Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
00123     TA.startTransaction();
00124   }
00125 
00126   ~Transaction() {
00127     if (!isAborted())
00128       TA.commitTransaction();
00129   }
00130 
00131   void abort() {
00132     TA.abortTransaction();
00133     Aborted = true;
00134   }
00135 
00136   bool isAborted() const { return Aborted; }
00137 };
00138 
00139 class MigrationPass {
00140 public:
00141   ASTContext &Ctx;
00142   LangOptions::GCMode OrigGCMode;
00143   MigratorOptions MigOptions;
00144   Sema &SemaRef;
00145   TransformActions &TA;
00146   std::vector<SourceLocation> &ARCMTMacroLocs;
00147 
00148   MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
00149                 Sema &sema, TransformActions &TA,
00150                 std::vector<SourceLocation> &ARCMTMacroLocs)
00151     : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
00152       SemaRef(sema), TA(TA),
00153       ARCMTMacroLocs(ARCMTMacroLocs) { }
00154 
00155   bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
00156   bool noNSAllocReallocError() const { return MigOptions.NoNSAllocReallocError; }
00157   void setNSAllocReallocError(bool val) { MigOptions.NoNSAllocReallocError = val; }
00158   bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
00159   void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
00160 };
00161 
00162 static inline StringRef getARCMTMacroName() {
00163   return "__IMPL_ARCMT_REMOVED_EXPR__";
00164 }
00165 
00166 } // end namespace arcmt
00167 
00168 } // end namespace clang
00169 
00170 #endif