18305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar//===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===// 28305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// 38305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// The LLVM Compiler Infrastructure 48305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// 58305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// This file is distributed under the University of Illinois Open Source 68305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// License. See LICENSE.TXT for details. 78305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar// 88305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar//===----------------------------------------------------------------------===// 98305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 108305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar#ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H 118305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar#define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H 128305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 138305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar#include "clang/Frontend/FrontendAction.h" 149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include <string> 159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include <vector> 168305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 178305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarnamespace clang { 188305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 191a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregorclass Module; 201a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor 215f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar//===----------------------------------------------------------------------===// 222758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar// Custom Consumer Actions 232758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar//===----------------------------------------------------------------------===// 242758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar 252758595023c5c7c0495f19260089f975022c50dcDaniel Dunbarclass InitOnlyAction : public FrontendAction { 262758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar virtual void ExecuteAction(); 272758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar 282758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 29686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 302758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar 312758595023c5c7c0495f19260089f975022c50dcDaniel Dunbarpublic: 322758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar // Don't claim to only use the preprocessor, we want to follow the AST path, 332758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar // but do nothing. 342758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar virtual bool usesPreprocessorOnly() const { return false; } 352758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar}; 362758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar 372758595023c5c7c0495f19260089f975022c50dcDaniel Dunbar//===----------------------------------------------------------------------===// 385f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar// AST Consumer Actions 395f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar//===----------------------------------------------------------------------===// 405f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 418305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass ASTPrintAction : public ASTFrontendAction { 428305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 438305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 44686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 458305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 468305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 478305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass ASTDumpAction : public ASTFrontendAction { 488305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 498305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 50686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 518305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 528305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 53171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienkoclass ASTDeclListAction : public ASTFrontendAction { 54171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienkoprotected: 55171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienko virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 56171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienko StringRef InFile); 57171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienko}; 58171af64ef10ae816ed2a4f15f5d17aca1d71769fAlexander Kornienko 59f35142413974e9dfe2c5f202084db4bd1c2073e9John McCallclass ASTDumpXMLAction : public ASTFrontendAction { 60f35142413974e9dfe2c5f202084db4bd1c2073e9John McCallprotected: 61f35142413974e9dfe2c5f202084db4bd1c2073e9John McCall virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 62686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 63f35142413974e9dfe2c5f202084db4bd1c2073e9John McCall}; 64f35142413974e9dfe2c5f202084db4bd1c2073e9John McCall 658305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass ASTViewAction : public ASTFrontendAction { 668305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 678305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 68686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 698305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 708305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 718305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass DeclContextPrintAction : public ASTFrontendAction { 728305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 738305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 74686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 758305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 768305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 778305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass GeneratePCHAction : public ASTFrontendAction { 788305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 798305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 80686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 818305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 82b80a3869b419c94ad85d2a39041c59100691bb3eBenjamin Kramer virtual TranslationUnitKind getTranslationUnitKind() { 83b80a3869b419c94ad85d2a39041c59100691bb3eBenjamin Kramer return TU_Prefix; 84467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor } 858305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 86eb58d831b283a9fa030a2eccc6e23480108d2fa1Daniel Dunbar virtual bool hasASTFileSupport() const { return false; } 871d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor 881d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregorpublic: 891d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor /// \brief Compute the AST consumer arguments that will be used to 901d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor /// create the PCHGenerator instance returned by CreateASTConsumer. 911d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor /// 921d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor /// \returns true if an error occurred, false otherwise. 931d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor static bool ComputeASTConsumerArguments(CompilerInstance &CI, 94686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile, 951d715ac14bf440664fb0d1425ea882274f994f57Douglas Gregor std::string &Sysroot, 968e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis std::string &OutputFile, 979293ba8e26fcba18505b273ecc9b66645133fcceDouglas Gregor raw_ostream *&OS); 988305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 998305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 100db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregorclass GenerateModuleAction : public ASTFrontendAction { 1018374eda250c1cf6aede92eca289759f5e0a6c308Douglas Gregor clang::Module *Module; 102a8cc6ce36e70e2afa22ab6b4340035cb3941c2ebDouglas Gregor 103db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregorprotected: 104db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 105db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor StringRef InFile); 106db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor 107db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor virtual TranslationUnitKind getTranslationUnitKind() { 108db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor return TU_Module; 109db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor } 110db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor 111db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor virtual bool hasASTFileSupport() const { return false; } 112db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor 113db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregorpublic: 114db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor virtual bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename); 115db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor 116db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor /// \brief Compute the AST consumer arguments that will be used to 117db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor /// create the PCHGenerator instance returned by CreateASTConsumer. 118db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor /// 119db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor /// \returns true if an error occurred, false otherwise. 120db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor static bool ComputeASTConsumerArguments(CompilerInstance &CI, 121db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor StringRef InFile, 122db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor std::string &Sysroot, 123db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor std::string &OutputFile, 124db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor raw_ostream *&OS); 125db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor}; 126db1cde7dc7bb3aaf48118bd9605192ab94a93635Douglas Gregor 1278305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarclass SyntaxOnlyAction : public ASTFrontendAction { 1288305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarprotected: 1298305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 130686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 1318305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 1328305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbarpublic: 1338305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar virtual bool hasCodeCompletionSupport() const { return true; } 1348305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar}; 1358305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 1369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor/** 1379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * \brief Frontend action adaptor that merges ASTs together. 1389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * 1399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * This action takes an existing AST file and "merges" it into the AST 1409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * context, producing a merged context. This action is an action 1419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * adaptor, which forwards most of its calls to another action that 1429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor * will consume the merged context. 1439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor */ 1449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorclass ASTMergeAction : public FrontendAction { 1459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor /// \brief The action that the merge action adapts. 1469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor FrontendAction *AdaptedAction; 1479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor /// \brief The set of AST files to merge. 1499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor std::vector<std::string> ASTFiles; 1509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorprotected: 1529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 153686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef InFile); 1549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual bool BeginSourceFileAction(CompilerInstance &CI, 156686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef Filename); 1579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual void ExecuteAction(); 1599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual void EndSourceFileAction(); 1609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorpublic: 162b3ca2637a9a3aeac5c7103eb4d612528680e7e20Argyrios Kyrtzidis ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef<std::string> ASTFiles); 1639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual ~ASTMergeAction(); 1649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 1659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual bool usesPreprocessorOnly() const; 166467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor virtual TranslationUnitKind getTranslationUnitKind(); 1679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual bool hasPCHSupport() const; 168eb58d831b283a9fa030a2eccc6e23480108d2fa1Daniel Dunbar virtual bool hasASTFileSupport() const; 1699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor virtual bool hasCodeCompletionSupport() const; 1709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}; 1719bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor 172f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregorclass PrintPreambleAction : public FrontendAction { 173f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregorprotected: 174f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor void ExecuteAction(); 175686775deca8b8685eb90801495880e3abdd844c2Chris Lattner virtual ASTConsumer *CreateASTConsumer(CompilerInstance &, StringRef) { 176f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor return 0; 177f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 178f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 179f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor virtual bool usesPreprocessorOnly() const { return true; } 180f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}; 181f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 1825f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar//===----------------------------------------------------------------------===// 1835f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar// Preprocessor Actions 1845f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar//===----------------------------------------------------------------------===// 1855f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 1865f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarclass DumpRawTokensAction : public PreprocessorFrontendAction { 1875f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarprotected: 1885f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar void ExecuteAction(); 1895f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar}; 1905f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 1915f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarclass DumpTokensAction : public PreprocessorFrontendAction { 1925f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarprotected: 1935f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar void ExecuteAction(); 1945f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar}; 1955f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 1965f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarclass GeneratePTHAction : public PreprocessorFrontendAction { 1975f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarprotected: 1985f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar void ExecuteAction(); 1995f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar}; 2005f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 2015f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarclass PreprocessOnlyAction : public PreprocessorFrontendAction { 2025f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarprotected: 2035f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar void ExecuteAction(); 2045f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar}; 2055f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 2065f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarclass PrintPreprocessedAction : public PreprocessorFrontendAction { 2075f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbarprotected: 2085f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar void ExecuteAction(); 2095f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar 2105f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar virtual bool hasPCHSupport() const { return true; } 2115f3b997e28899972e2ba23ec25e830d4066fa59aDaniel Dunbar}; 212f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 2138305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar} // end namespace clang 2148305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar 2158305d01a1de4bfbfdd501b83074a1e80fada752dDaniel Dunbar#endif 216