2020#include " LLVMMisc.hh"
2121
2222#include < clang/AST/Attr.h>
23+ #include < llvm/Support/Regex.h>
2324
2425/* * Public methods. */
2526
2627#define Out (*Out)
2728
28- void PrettyPrint::Print_Decl (Decl *decl)
29+ void PrettyPrint::Print_Decl (Decl *decl, bool keep_includes )
2930{
3031 /* When handling C code, we need to check if given declaration is a function
3132 with body. If yes, we can simply print the declaration, but otherwise
@@ -38,22 +39,22 @@ void PrettyPrint::Print_Decl(Decl *decl)
3839 if (f && f->hasBody () && f->isThisDeclarationADefinition ()) {
3940 Print_Decl_Raw (f);
4041 Out << " \n\n " ;
41- } else if (e) {
42- decl->print (Out, PPolicy);
43- Out << " ;\n\n " ;
4442 } else if (!e && t && t->getName () == " " ) {
4543 /* If the RecordType doesn't have a name, then don't print it. Except when
4644 it is an empty named enum declaration, which in this case we must print
4745 because it contains declared constants. */
4846 } else {
49- /* Structs and prototypes */
47+ /* Structs, enums, and prototypes */
5048
51- /* * Check if we can get a partial declaration of `decl` rather than a full
52- declaration. */
53- bool full_def_removed = false ;
54- if (t && t->isCompleteDefinitionRequired () == false ) {
55- /* We don't need the full defintion. Hide the body for Print_Decl_Raw. */
56- t->setCompleteDefinition (false );
49+ /* * Check if the output string has an include directive. In that case, if we
50+ don't want to keep the includes, we must print the ast dump rather than
51+ the source text to get rid of the #include directive.
52+ FIXME: regexes are slow. */
53+ static llvm::Regex regex (" # *include *(<|\" )" );
54+
55+ /* FIXME: Why isCompleteDefinitionRequired does not work for EnumDecls? */
56+ if (t && ((!e && t->isCompleteDefinitionRequired () == false )
57+ || (!keep_includes && regex.match (Get_Source_Text (t->getSourceRange ()))))) {
5758
5859 /* FIXME: The Print_Decl_Raw class will attempt to write this declaration
5960 as the user wrote, that means WITH a body. To avoid this, we set
@@ -63,7 +64,15 @@ void PrettyPrint::Print_Decl(Decl *decl)
6364 correct way of doing this would be update the source location to
6465 the correct range. */
6566 t->setLocStart (t->getEndLoc ());
67+ }
6668
69+ /* * Check if we can get a partial declaration of `decl` rather than a full
70+ declaration. */
71+ bool full_def_removed = false ;
72+ /* FIXME: Why isCompleteDefinitionRequired does not work for EnumDecls? */
73+ if (t && !e && t->isCompleteDefinitionRequired () == false ) {
74+ /* We don't need the full defintion. Hide the body for Print_Decl_Raw. */
75+ t->setCompleteDefinition (false );
6776 full_def_removed = true ;
6877 }
6978
@@ -613,7 +622,7 @@ void RecursivePrint::Print_Decl(Decl *decl)
613622 PrettyPrint::Print_RawComment (sm, comment);
614623 }
615624 }
616- PrettyPrint::Print_Decl (decl);
625+ PrettyPrint::Print_Decl (decl, KeepIncludes );
617626 }
618627}
619628
@@ -656,13 +665,7 @@ void RecursivePrint::Print(void)
656665 decl = (*ASTIterator).AsDecl ;
657666 Print_Decl (decl);
658667 ++ASTIterator;
659-
660- /* Skip to the end of the Declaration. */
661- if (!dyn_cast<EnumDecl>(decl)) {
662- /* EnumDecls are handled somewhat differently: we dump the AST, not
663- what the user wrote. */
664- ASTIterator.Skip_Until (decl->getEndLoc ());
665- }
668+ ASTIterator.Skip_Until (decl->getEndLoc ());
666669 break ;
667670
668671 case TopLevelASTIterator::ReturnType::TYPE_PREPROCESSED_ENTITY:
0 commit comments