File indexing completed on 2024-06-23 04:35:53
0001 // astyle.h 0002 // Copyright (c) 2018 by Jim Pattee <jimp03@email.com>. 0003 // This code is licensed under the MIT License. 0004 // License.md describes the conditions under which this software may be distributed. 0005 0006 #ifndef ASTYLE_H 0007 #define ASTYLE_H 0008 0009 //----------------------------------------------------------------------------- 0010 // headers 0011 //----------------------------------------------------------------------------- 0012 0013 #ifdef __VMS 0014 #define __USE_STD_IOSTREAM 1 0015 #include <assert> 0016 #else 0017 #include <cassert> 0018 #endif 0019 0020 #include <cctype> 0021 #include <iostream> // for cout 0022 #include <memory> 0023 #include <string> 0024 #include <vector> 0025 0026 #ifdef __GNUC__ 0027 #include <cstring> // need both string and cstring for GCC 0028 #endif 0029 0030 //----------------------------------------------------------------------------- 0031 // declarations 0032 //----------------------------------------------------------------------------- 0033 0034 #ifdef _MSC_VER 0035 #pragma warning(disable: 4267) // conversion from size_t to int 0036 #endif 0037 0038 #ifdef __BORLANDC__ 0039 #pragma warn -8004 // variable is assigned a value that is never used 0040 #endif 0041 0042 #ifdef __GNUC__ 0043 #pragma GCC diagnostic ignored "-Wconversion" 0044 #endif 0045 0046 #ifdef __INTEL_COMPILER 0047 #pragma warning(disable: 383) // value copied to temporary, reference to temporary used 0048 #pragma warning(disable: 981) // operands are evaluated in unspecified order 0049 #endif 0050 0051 #ifdef __clang__ 0052 #pragma clang diagnostic ignored "-Wshorten-64-to-32" 0053 #endif 0054 0055 //----------------------------------------------------------------------------- 0056 // astyle namespace 0057 //----------------------------------------------------------------------------- 0058 0059 namespace astyle { 0060 // 0061 using namespace std; 0062 0063 //---------------------------------------------------------------------------- 0064 // definitions 0065 //---------------------------------------------------------------------------- 0066 0067 enum FileType { C_TYPE = 0, JAVA_TYPE = 1, SHARP_TYPE = 2 }; 0068 0069 /* The enums below are not recognized by 'vectors' in Microsoft Visual C++ 0070 V5 when they are part of a namespace!!! Use Visual C++ V6 or higher. 0071 */ 0072 enum FormatStyle 0073 { 0074 STYLE_NONE, 0075 STYLE_ALLMAN, 0076 STYLE_JAVA, 0077 STYLE_KR, 0078 STYLE_STROUSTRUP, 0079 STYLE_WHITESMITH, 0080 STYLE_VTK, 0081 STYLE_RATLIFF, 0082 STYLE_GNU, 0083 STYLE_LINUX, 0084 STYLE_HORSTMANN, 0085 STYLE_1TBS, 0086 STYLE_GOOGLE, 0087 STYLE_MOZILLA, 0088 STYLE_PICO, 0089 STYLE_LISP 0090 }; 0091 0092 enum BraceMode 0093 { 0094 NONE_MODE, 0095 ATTACH_MODE, 0096 BREAK_MODE, 0097 LINUX_MODE, 0098 RUN_IN_MODE // broken braces 0099 }; 0100 0101 // maximum value for int is 16,384 (total value of 32,767) 0102 enum BraceType 0103 { 0104 NULL_TYPE = 0, 0105 NAMESPACE_TYPE = 1, // also a DEFINITION_TYPE 0106 CLASS_TYPE = 2, // also a DEFINITION_TYPE 0107 STRUCT_TYPE = 4, // also a DEFINITION_TYPE 0108 INTERFACE_TYPE = 8, // also a DEFINITION_TYPE 0109 DEFINITION_TYPE = 16, 0110 COMMAND_TYPE = 32, 0111 ARRAY_NIS_TYPE = 64, // also an ARRAY_TYPE 0112 ENUM_TYPE = 128, // also an ARRAY_TYPE 0113 INIT_TYPE = 256, // also an ARRAY_TYPE 0114 ARRAY_TYPE = 512, 0115 EXTERN_TYPE = 1024, // extern "C", not a command type extern 0116 EMPTY_BLOCK_TYPE = 2048, // also a SINGLE_LINE_TYPE 0117 BREAK_BLOCK_TYPE = 4096, // also a SINGLE_LINE_TYPE 0118 SINGLE_LINE_TYPE = 8192 0119 }; 0120 0121 enum MinConditional 0122 { 0123 MINCOND_ZERO, 0124 MINCOND_ONE, 0125 MINCOND_TWO, 0126 MINCOND_ONEHALF, 0127 MINCOND_END 0128 }; 0129 0130 enum ObjCColonPad 0131 { 0132 COLON_PAD_NO_CHANGE, 0133 COLON_PAD_NONE, 0134 COLON_PAD_ALL, 0135 COLON_PAD_AFTER, 0136 COLON_PAD_BEFORE 0137 }; 0138 0139 enum PointerAlign 0140 { 0141 PTR_ALIGN_NONE, 0142 PTR_ALIGN_TYPE, 0143 PTR_ALIGN_MIDDLE, 0144 PTR_ALIGN_NAME 0145 }; 0146 0147 enum ReferenceAlign 0148 { 0149 REF_ALIGN_NONE = PTR_ALIGN_NONE, 0150 REF_ALIGN_TYPE = PTR_ALIGN_TYPE, 0151 REF_ALIGN_MIDDLE = PTR_ALIGN_MIDDLE, 0152 REF_ALIGN_NAME = PTR_ALIGN_NAME, 0153 REF_SAME_AS_PTR 0154 }; 0155 0156 enum FileEncoding 0157 { 0158 ENCODING_8BIT, // includes UTF-8 without BOM 0159 UTF_8BOM, // UTF-8 with BOM 0160 UTF_16BE, 0161 UTF_16LE, // Windows default 0162 UTF_32BE, 0163 UTF_32LE 0164 }; 0165 0166 enum LineEndFormat 0167 { 0168 LINEEND_DEFAULT, // Use line break that matches most of the file 0169 LINEEND_WINDOWS, 0170 LINEEND_LINUX, 0171 LINEEND_MACOLD, 0172 LINEEND_CRLF = LINEEND_WINDOWS, 0173 LINEEND_LF = LINEEND_LINUX, 0174 LINEEND_CR = LINEEND_MACOLD 0175 }; 0176 0177 //----------------------------------------------------------------------------- 0178 // Class ASSourceIterator 0179 // A pure virtual class is used by ASFormatter and ASBeautifier instead of 0180 // ASStreamIterator. This allows programs using AStyle as a plug-in to define 0181 // their own ASStreamIterator. The ASStreamIterator class must inherit 0182 // this class. 0183 //----------------------------------------------------------------------------- 0184 0185 class ASSourceIterator 0186 { 0187 public: 0188 ASSourceIterator() {} 0189 virtual ~ASSourceIterator() {} 0190 virtual streamoff getPeekStart() const = 0; 0191 virtual int getStreamLength() const = 0; 0192 virtual bool hasMoreLines() const = 0; 0193 virtual string nextLine(bool emptyLineWasDeleted = false) = 0; 0194 virtual string peekNextLine() = 0; 0195 virtual void peekReset() = 0; 0196 virtual streamoff tellg() = 0; 0197 }; 0198 0199 //----------------------------------------------------------------------------- 0200 // Class ASPeekStream 0201 // A small class using RAII to peek ahead in the ASSourceIterator stream 0202 // and to reset the ASSourceIterator pointer in the destructor. 0203 // It enables a return from anywhere in the method. 0204 //----------------------------------------------------------------------------- 0205 0206 class ASPeekStream 0207 { 0208 private: 0209 ASSourceIterator* sourceIterator; 0210 bool needReset; // reset sourceIterator to the original position 0211 0212 public: 0213 explicit ASPeekStream(ASSourceIterator* sourceIterator_) 0214 { sourceIterator = sourceIterator_; needReset = false; } 0215 0216 ~ASPeekStream() 0217 { if (needReset) sourceIterator->peekReset(); } 0218 0219 bool hasMoreLines() const 0220 { return sourceIterator->hasMoreLines(); } 0221 0222 string peekNextLine() 0223 { needReset = true; return sourceIterator->peekNextLine(); } 0224 }; 0225 0226 0227 //----------------------------------------------------------------------------- 0228 // Class ASResource 0229 //----------------------------------------------------------------------------- 0230 0231 class ASResource 0232 { 0233 public: 0234 void buildAssignmentOperators(vector<const string*>* assignmentOperators); 0235 void buildCastOperators(vector<const string*>* castOperators); 0236 void buildHeaders(vector<const string*>* headers, int fileType, bool beautifier = false); 0237 void buildIndentableMacros(vector<const pair<const string, const string>* >* indentableMacros); 0238 void buildIndentableHeaders(vector<const string*>* indentableHeaders); 0239 void buildNonAssignmentOperators(vector<const string*>* nonAssignmentOperators); 0240 void buildNonParenHeaders(vector<const string*>* nonParenHeaders, int fileType, bool beautifier = false); 0241 void buildOperators(vector<const string*>* operators, int fileType); 0242 void buildPreBlockStatements(vector<const string*>* preBlockStatements, int fileType); 0243 void buildPreCommandHeaders(vector<const string*>* preCommandHeaders, int fileType); 0244 void buildPreDefinitionHeaders(vector<const string*>* preDefinitionHeaders, int fileType); 0245 0246 public: 0247 static const string AS_IF, AS_ELSE; 0248 static const string AS_DO, AS_WHILE; 0249 static const string AS_FOR; 0250 static const string AS_SWITCH, AS_CASE, AS_DEFAULT; 0251 static const string AS_TRY, AS_CATCH, AS_THROW, AS_THROWS, AS_FINALLY, AS_USING; 0252 static const string _AS_TRY, _AS_FINALLY, _AS_EXCEPT; 0253 static const string AS_PUBLIC, AS_PROTECTED, AS_PRIVATE; 0254 static const string AS_CLASS, AS_STRUCT, AS_UNION, AS_INTERFACE, AS_NAMESPACE; 0255 static const string AS_MODULE; 0256 static const string AS_END; 0257 static const string AS_SELECTOR; 0258 static const string AS_EXTERN, AS_ENUM; 0259 static const string AS_FINAL, AS_OVERRIDE; 0260 static const string AS_STATIC, AS_CONST, AS_SEALED, AS_VOLATILE, AS_NEW, AS_DELETE; 0261 static const string AS_NOEXCEPT, AS_INTERRUPT, AS_AUTORELEASEPOOL; 0262 static const string AS_WHERE, AS_LET, AS_SYNCHRONIZED; 0263 static const string AS_OPERATOR, AS_TEMPLATE; 0264 static const string AS_OPEN_BRACE, AS_CLOSE_BRACE; 0265 static const string AS_OPEN_LINE_COMMENT, AS_OPEN_COMMENT, AS_CLOSE_COMMENT; 0266 static const string AS_BAR_DEFINE, AS_BAR_INCLUDE, AS_BAR_IF, AS_BAR_EL, AS_BAR_ENDIF; 0267 static const string AS_AUTO, AS_RETURN; 0268 static const string AS_CIN, AS_COUT, AS_CERR; 0269 static const string AS_ASSIGN, AS_PLUS_ASSIGN, AS_MINUS_ASSIGN, AS_MULT_ASSIGN; 0270 static const string AS_DIV_ASSIGN, AS_MOD_ASSIGN, AS_XOR_ASSIGN, AS_OR_ASSIGN, AS_AND_ASSIGN; 0271 static const string AS_GR_GR_ASSIGN, AS_LS_LS_ASSIGN, AS_GR_GR_GR_ASSIGN, AS_LS_LS_LS_ASSIGN; 0272 static const string AS_GCC_MIN_ASSIGN, AS_GCC_MAX_ASSIGN; 0273 static const string AS_EQUAL, AS_PLUS_PLUS, AS_MINUS_MINUS, AS_NOT_EQUAL, AS_GR_EQUAL; 0274 static const string AS_LS_EQUAL, AS_LS_LS_LS, AS_LS_LS, AS_GR_GR_GR, AS_GR_GR; 0275 static const string AS_QUESTION_QUESTION, AS_LAMBDA; 0276 static const string AS_ARROW, AS_AND, AS_OR; 0277 static const string AS_SCOPE_RESOLUTION; 0278 static const string AS_PLUS, AS_MINUS, AS_MULT, AS_DIV, AS_MOD, AS_GR, AS_LS; 0279 static const string AS_NOT, AS_BIT_XOR, AS_BIT_OR, AS_BIT_AND, AS_BIT_NOT; 0280 static const string AS_QUESTION, AS_COLON, AS_SEMICOLON, AS_COMMA; 0281 static const string AS_ASM, AS__ASM__, AS_MS_ASM, AS_MS__ASM; 0282 static const string AS_QFOREACH, AS_QFOREVER, AS_FOREVER; 0283 static const string AS_FOREACH, AS_LOCK, AS_UNSAFE, AS_FIXED; 0284 static const string AS_GET, AS_SET, AS_ADD, AS_REMOVE; 0285 static const string AS_DELEGATE, AS_UNCHECKED; 0286 static const string AS_CONST_CAST, AS_DYNAMIC_CAST, AS_REINTERPRET_CAST, AS_STATIC_CAST; 0287 static const string AS_NS_DURING, AS_NS_HANDLER; 0288 }; // Class ASResource 0289 0290 //----------------------------------------------------------------------------- 0291 // Class ASBase 0292 // Functions definitions are at the end of ASResource.cpp. 0293 //----------------------------------------------------------------------------- 0294 0295 class ASBase : protected ASResource 0296 { 0297 private: 0298 // all variables should be set by the "init" function 0299 int baseFileType; // a value from enum FileType 0300 0301 protected: 0302 ASBase() : baseFileType(C_TYPE) { } 0303 0304 protected: // inline functions 0305 void init(int fileTypeArg) { baseFileType = fileTypeArg; } 0306 bool isCStyle() const { return (baseFileType == C_TYPE); } 0307 bool isJavaStyle() const { return (baseFileType == JAVA_TYPE); } 0308 bool isSharpStyle() const { return (baseFileType == SHARP_TYPE); } 0309 bool isWhiteSpace(char ch) const { return (ch == ' ' || ch == '\t'); } 0310 0311 protected: // functions definitions are at the end of ASResource.cpp 0312 const string* findHeader(const string& line, int i, 0313 const vector<const string*>* possibleHeaders) const; 0314 bool findKeyword(const string& line, int i, const string& keyword) const; 0315 const string* findOperator(const string& line, int i, 0316 const vector<const string*>* possibleOperators) const; 0317 string getCurrentWord(const string& line, size_t index) const; 0318 bool isDigit(char ch) const; 0319 bool isLegalNameChar(char ch) const; 0320 bool isCharPotentialHeader(const string& line, size_t i) const; 0321 bool isCharPotentialOperator(char ch) const; 0322 bool isDigitSeparator(const string& line, int i) const; 0323 char peekNextChar(const string& line, int i) const; 0324 0325 }; // Class ASBase 0326 0327 //----------------------------------------------------------------------------- 0328 // Class ASBeautifier 0329 //----------------------------------------------------------------------------- 0330 0331 class ASBeautifier : protected ASBase 0332 { 0333 public: 0334 ASBeautifier(); 0335 virtual ~ASBeautifier(); 0336 virtual void init(ASSourceIterator* iter); 0337 virtual string beautify(const string& originalLine); 0338 void setCaseIndent(bool state); 0339 void setClassIndent(bool state); 0340 void setContinuationIndentation(int indent = 1); 0341 void setCStyle(); 0342 void setDefaultTabLength(); 0343 void setEmptyLineFill(bool state); 0344 void setForceTabXIndentation(int length); 0345 void setAfterParenIndent(bool state); 0346 void setJavaStyle(); 0347 void setLabelIndent(bool state); 0348 void setMaxContinuationIndentLength(int max); 0349 void setMaxInStatementIndentLength(int max); 0350 void setMinConditionalIndentOption(int min); 0351 void setMinConditionalIndentLength(); 0352 void setModeManuallySet(bool state); 0353 void setModifierIndent(bool state); 0354 void setNamespaceIndent(bool state); 0355 void setAlignMethodColon(bool state); 0356 void setSharpStyle(); 0357 void setSpaceIndentation(int length = 4); 0358 void setSwitchIndent(bool state); 0359 void setTabIndentation(int length = 4, bool forceTabs = false); 0360 void setPreprocDefineIndent(bool state); 0361 void setPreprocConditionalIndent(bool state); 0362 int getBeautifierFileType() const; 0363 int getFileType() const; 0364 int getIndentLength() const; 0365 int getTabLength() const; 0366 string getIndentString() const; 0367 string getNextWord(const string& line, size_t currPos) const; 0368 bool getAlignMethodColon() const; 0369 bool getBraceIndent() const; 0370 bool getBlockIndent() const; 0371 bool getCaseIndent() const; 0372 bool getClassIndent() const; 0373 bool getEmptyLineFill() const; 0374 bool getForceTabIndentation() const; 0375 bool getModeManuallySet() const; 0376 bool getModifierIndent() const; 0377 bool getNamespaceIndent() const; 0378 bool getPreprocDefineIndent() const; 0379 bool getSwitchIndent() const; 0380 0381 protected: 0382 void deleteBeautifierVectors(); 0383 int getNextProgramCharDistance(const string& line, int i) const; 0384 int indexOf(const vector<const string*>& container, const string* element) const; 0385 void setBlockIndent(bool state); 0386 void setBraceIndent(bool state); 0387 void setBraceIndentVtk(bool state); 0388 string extractPreprocessorStatement(const string& line) const; 0389 string trim(const string& str) const; 0390 string rtrim(const string& str) const; 0391 0392 // variables set by ASFormatter - must be updated in activeBeautifierStack 0393 int inLineNumber; 0394 int runInIndentContinuation; 0395 int nonInStatementBrace; 0396 int objCColonAlignSubsequent; // for subsequent lines not counting indent 0397 bool lineCommentNoBeautify; 0398 bool isElseHeaderIndent; 0399 bool isCaseHeaderCommentIndent; 0400 bool isNonInStatementArray; 0401 bool isSharpAccessor; 0402 bool isSharpDelegate; 0403 bool isInExternC; 0404 bool isInBeautifySQL; 0405 bool isInIndentableStruct; 0406 bool isInIndentablePreproc; 0407 0408 private: // functions 0409 ASBeautifier(const ASBeautifier& other); // inline functions 0410 ASBeautifier& operator=(ASBeautifier&); // not to be implemented 0411 0412 void adjustObjCMethodDefinitionIndentation(const string& line_); 0413 void adjustObjCMethodCallIndentation(const string& line_); 0414 void adjustParsedLineIndentation(size_t iPrelim, bool isInExtraHeaderIndent); 0415 void computePreliminaryIndentation(); 0416 void parseCurrentLine(const string& line); 0417 void popLastContinuationIndent(); 0418 void processPreprocessor(const string& preproc, const string& line); 0419 void registerContinuationIndent(const string& line, int i, int spaceIndentCount_, 0420 int tabIncrementIn, int minIndent, bool updateParenStack); 0421 void registerContinuationIndentColon(const string& line, int i, int tabIncrementIn); 0422 void initVectors(); 0423 void initTempStacksContainer(vector<vector<const string*>*>*& container, 0424 vector<vector<const string*>*>* value); 0425 void clearObjCMethodDefinitionAlignment(); 0426 void deleteBeautifierContainer(vector<ASBeautifier*>*& container); 0427 void deleteTempStacksContainer(vector<vector<const string*>*>*& container); 0428 int adjustIndentCountForBreakElseIfComments() const; 0429 int computeObjCColonAlignment(const string& line, int colonAlignPosition) const; 0430 int convertTabToSpaces(int i, int tabIncrementIn) const; 0431 int findObjCColonAlignment(const string& line) const; 0432 int getContinuationIndentAssign(const string& line, size_t currPos) const; 0433 int getContinuationIndentComma(const string& line, size_t currPos) const; 0434 int getObjCFollowingKeyword(const string& line, int bracePos) const; 0435 bool isIndentedPreprocessor(const string& line, size_t currPos) const; 0436 bool isLineEndComment(const string& line, int startPos) const; 0437 bool isPreprocessorConditionalCplusplus(const string& line) const; 0438 bool isInPreprocessorUnterminatedComment(const string& line); 0439 bool isTopLevel() const; 0440 bool statementEndsWithComma(const string& line, int index) const; 0441 const string& getIndentedLineReturn(const string& newLine, const string& originalLine) const; 0442 string getIndentedSpaceEquivalent(const string& line_) const; 0443 string preLineWS(int lineIndentCount, int lineSpaceIndentCount) const; 0444 template<typename T> void deleteContainer(T& container); 0445 template<typename T> void initContainer(T& container, T value); 0446 vector<vector<const string*>*>* copyTempStacks(const ASBeautifier& other) const; 0447 pair<int, int> computePreprocessorIndent(); 0448 0449 private: // variables 0450 int beautifierFileType; 0451 vector<const string*>* headers; 0452 vector<const string*>* nonParenHeaders; 0453 vector<const string*>* preBlockStatements; 0454 vector<const string*>* preCommandHeaders; 0455 vector<const string*>* assignmentOperators; 0456 vector<const string*>* nonAssignmentOperators; 0457 vector<const string*>* indentableHeaders; 0458 0459 vector<ASBeautifier*>* waitingBeautifierStack; 0460 vector<ASBeautifier*>* activeBeautifierStack; 0461 vector<int>* waitingBeautifierStackLengthStack; 0462 vector<int>* activeBeautifierStackLengthStack; 0463 vector<const string*>* headerStack; 0464 vector<vector<const string*>* >* tempStacks; 0465 vector<int>* parenDepthStack; 0466 vector<bool>* blockStatementStack; 0467 vector<bool>* parenStatementStack; 0468 vector<bool>* braceBlockStateStack; 0469 vector<int>* continuationIndentStack; 0470 vector<int>* continuationIndentStackSizeStack; 0471 vector<int>* parenIndentStack; 0472 vector<pair<int, int> >* preprocIndentStack; 0473 0474 ASSourceIterator* sourceIterator; 0475 const string* currentHeader; 0476 const string* previousLastLineHeader; 0477 const string* probationHeader; 0478 const string* lastLineHeader; 0479 string indentString; 0480 string verbatimDelimiter; 0481 bool isInQuote; 0482 bool isInVerbatimQuote; 0483 bool haveLineContinuationChar; 0484 bool isInAsm; 0485 bool isInAsmOneLine; 0486 bool isInAsmBlock; 0487 bool isInComment; 0488 bool isInPreprocessorComment; 0489 bool isInRunInComment; 0490 bool isInCase; 0491 bool isInQuestion; 0492 bool isContinuation; 0493 bool isInHeader; 0494 bool isInTemplate; 0495 bool isInDefine; 0496 bool isInDefineDefinition; 0497 bool classIndent; 0498 bool isIndentModeOff; 0499 bool isInClassHeader; // is in a class before the opening brace 0500 bool isInClassHeaderTab; // is in an indentable class header line 0501 bool isInClassInitializer; // is in a class after the ':' initializer 0502 bool isInClass; // is in a class after the opening brace 0503 bool isInObjCMethodDefinition; 0504 bool isInObjCMethodCall; 0505 bool isInObjCMethodCallFirst; 0506 bool isImmediatelyPostObjCMethodDefinition; 0507 bool isImmediatelyPostObjCMethodCall; 0508 bool isInIndentablePreprocBlock; 0509 bool isInObjCInterface; 0510 bool isInEnum; 0511 bool isInEnumTypeID; 0512 bool isInLet; 0513 bool isInTrailingReturnType; 0514 bool modifierIndent; 0515 bool switchIndent; 0516 bool caseIndent; 0517 bool namespaceIndent; 0518 bool blockIndent; 0519 bool braceIndent; 0520 bool braceIndentVtk; 0521 bool shouldIndentAfterParen; 0522 bool labelIndent; 0523 bool shouldIndentPreprocDefine; 0524 bool isInConditional; 0525 bool isModeManuallySet; 0526 bool shouldForceTabIndentation; 0527 bool emptyLineFill; 0528 bool backslashEndsPrevLine; 0529 bool lineOpensWithLineComment; 0530 bool lineOpensWithComment; 0531 bool lineStartsInComment; 0532 bool blockCommentNoIndent; 0533 bool blockCommentNoBeautify; 0534 bool previousLineProbationTab; 0535 bool lineBeginsWithOpenBrace; 0536 bool lineBeginsWithCloseBrace; 0537 bool lineBeginsWithComma; 0538 bool lineIsCommentOnly; 0539 bool lineIsLineCommentOnly; 0540 bool shouldIndentBracedLine; 0541 bool isInSwitch; 0542 bool foundPreCommandHeader; 0543 bool foundPreCommandMacro; 0544 bool shouldAlignMethodColon; 0545 bool shouldIndentPreprocConditional; 0546 int indentCount; 0547 int spaceIndentCount; 0548 int spaceIndentObjCMethodAlignment; 0549 int bracePosObjCMethodAlignment; 0550 int colonIndentObjCMethodAlignment; 0551 int lineOpeningBlocksNum; 0552 int lineClosingBlocksNum; 0553 int fileType; 0554 int minConditionalOption; 0555 int minConditionalIndent; 0556 int parenDepth; 0557 int indentLength; 0558 int tabLength; 0559 int continuationIndent; 0560 int blockTabCount; 0561 int maxContinuationIndent; 0562 int classInitializerIndents; 0563 int templateDepth; 0564 int squareBracketCount; 0565 int prevFinalLineSpaceIndentCount; 0566 int prevFinalLineIndentCount; 0567 int defineIndentCount; 0568 int preprocBlockIndent; 0569 char quoteChar; 0570 char prevNonSpaceCh; 0571 char currentNonSpaceCh; 0572 char currentNonLegalCh; 0573 char prevNonLegalCh; 0574 }; // Class ASBeautifier 0575 0576 //----------------------------------------------------------------------------- 0577 // Class ASEnhancer 0578 //----------------------------------------------------------------------------- 0579 0580 class ASEnhancer : protected ASBase 0581 { 0582 public: // functions 0583 ASEnhancer(); 0584 virtual ~ASEnhancer(); 0585 void init(int, int, int, bool, bool, bool, bool, bool, bool, bool, 0586 vector<const pair<const string, const string>* >*); 0587 void enhance(string& line, bool isInNamespace, bool isInPreprocessor, bool isInSQL); 0588 0589 private: // functions 0590 void convertForceTabIndentToSpaces(string& line) const; 0591 void convertSpaceIndentToForceTab(string& line) const; 0592 size_t findCaseColon(const string& line, size_t caseIndex) const; 0593 int indentLine(string& line, int indent) const; 0594 bool isBeginDeclareSectionSQL(const string& line, size_t index) const; 0595 bool isEndDeclareSectionSQL(const string& line, size_t index) const; 0596 bool isOneLineBlockReached(const string& line, int startChar) const; 0597 void parseCurrentLine(string& line, bool isInPreprocessor, bool isInSQL); 0598 size_t processSwitchBlock(string& line, size_t index); 0599 int unindentLine(string& line, int unindent) const; 0600 0601 private: 0602 // options from command line or options file 0603 int indentLength; 0604 int tabLength; 0605 bool useTabs; 0606 bool forceTab; 0607 bool namespaceIndent; 0608 bool caseIndent; 0609 bool preprocBlockIndent; 0610 bool preprocDefineIndent; 0611 bool emptyLineFill; 0612 0613 // parsing variables 0614 int lineNumber; 0615 bool isInQuote; 0616 bool isInComment; 0617 char quoteChar; 0618 0619 // unindent variables 0620 int braceCount; 0621 int switchDepth; 0622 int eventPreprocDepth; 0623 bool lookingForCaseBrace; 0624 bool unindentNextLine; 0625 bool shouldUnindentLine; 0626 bool shouldUnindentComment; 0627 0628 // struct used by ParseFormattedLine function 0629 // contains variables used to unindent the case blocks 0630 struct SwitchVariables 0631 { 0632 int switchBraceCount; 0633 int unindentDepth; 0634 bool unindentCase; 0635 }; 0636 0637 SwitchVariables sw; // switch variables struct 0638 vector<SwitchVariables> switchStack; // stack vector of switch variables 0639 0640 // event table variables 0641 bool nextLineIsEventIndent; // begin event table indent is reached 0642 bool isInEventTable; // need to indent an event table 0643 vector<const pair<const string, const string>* >* indentableMacros; 0644 0645 // SQL variables 0646 bool nextLineIsDeclareIndent; // begin declare section indent is reached 0647 bool isInDeclareSection; // need to indent a declare section 0648 0649 }; // Class ASEnhancer 0650 0651 //----------------------------------------------------------------------------- 0652 // Class ASFormatter 0653 //----------------------------------------------------------------------------- 0654 0655 class ASFormatter : public ASBeautifier 0656 { 0657 public: // functions 0658 ASFormatter(); 0659 virtual ~ASFormatter(); 0660 virtual void init(ASSourceIterator* si); 0661 virtual bool hasMoreLines() const; 0662 virtual string nextLine(); 0663 LineEndFormat getLineEndFormat() const; 0664 bool getIsLineReady() const; 0665 void setFormattingStyle(FormatStyle style); 0666 void setAddBracesMode(bool state); 0667 void setAddOneLineBracesMode(bool state); 0668 void setRemoveBracesMode(bool state); 0669 void setAttachClass(bool state); 0670 void setAttachClosingWhile(bool state); 0671 void setAttachExternC(bool state); 0672 void setAttachNamespace(bool state); 0673 void setAttachInline(bool state); 0674 void setBraceFormatMode(BraceMode mode); 0675 void setBreakAfterMode(bool state); 0676 void setBreakClosingHeaderBracesMode(bool state); 0677 void setBreakBlocksMode(bool state); 0678 void setBreakClosingHeaderBlocksMode(bool state); 0679 void setBreakElseIfsMode(bool state); 0680 void setBreakOneLineBlocksMode(bool state); 0681 void setBreakOneLineHeadersMode(bool state); 0682 void setBreakOneLineStatementsMode(bool state); 0683 void setMethodPrefixPaddingMode(bool state); 0684 void setMethodPrefixUnPaddingMode(bool state); 0685 void setReturnTypePaddingMode(bool state); 0686 void setReturnTypeUnPaddingMode(bool state); 0687 void setParamTypePaddingMode(bool state); 0688 void setParamTypeUnPaddingMode(bool state); 0689 void setCloseTemplatesMode(bool state); 0690 void setCommaPaddingMode(bool state); 0691 void setDeleteEmptyLinesMode(bool state); 0692 void setBreakReturnType(bool state); 0693 void setBreakReturnTypeDecl(bool state); 0694 void setAttachReturnType(bool state); 0695 void setAttachReturnTypeDecl(bool state); 0696 void setIndentCol1CommentsMode(bool state); 0697 void setLineEndFormat(LineEndFormat fmt); 0698 void setMaxCodeLength(int max); 0699 void setObjCColonPaddingMode(ObjCColonPad mode); 0700 void setOperatorPaddingMode(bool state); 0701 void setParensOutsidePaddingMode(bool state); 0702 void setParensFirstPaddingMode(bool state); 0703 void setParensInsidePaddingMode(bool state); 0704 void setParensHeaderPaddingMode(bool state); 0705 void setParensUnPaddingMode(bool state); 0706 void setPointerAlignment(PointerAlign alignment); 0707 void setPreprocBlockIndent(bool state); 0708 void setReferenceAlignment(ReferenceAlign alignment); 0709 void setStripCommentPrefix(bool state); 0710 void setTabSpaceConversionMode(bool state); 0711 size_t getChecksumIn() const; 0712 size_t getChecksumOut() const; 0713 int getChecksumDiff() const; 0714 int getFormatterFileType() const; 0715 // retained for compatibility with release 2.06 0716 // "Brackets" have been changed to "Braces" in 3.0 0717 // they are referenced only by the old "bracket" options 0718 void setAddBracketsMode(bool state); 0719 void setAddOneLineBracketsMode(bool state); 0720 void setRemoveBracketsMode(bool state); 0721 void setBreakClosingHeaderBracketsMode(bool state); 0722 0723 0724 private: // functions 0725 ASFormatter(const ASFormatter& copy); // not to be implemented 0726 ASFormatter& operator=(ASFormatter&); // not to be implemented 0727 template<typename T> void deleteContainer(T& container); 0728 template<typename T> void initContainer(T& container, T value); 0729 char peekNextChar() const; 0730 BraceType getBraceType(); 0731 bool adjustChecksumIn(int adjustment); 0732 bool computeChecksumIn(const string& currentLine_); 0733 bool computeChecksumOut(const string& beautifiedLine); 0734 bool addBracesToStatement(); 0735 bool removeBracesFromStatement(); 0736 bool commentAndHeaderFollows(); 0737 bool getNextChar(); 0738 bool getNextLine(bool emptyLineWasDeleted = false); 0739 bool isArrayOperator() const; 0740 bool isBeforeComment() const; 0741 bool isBeforeAnyComment() const; 0742 bool isBeforeAnyLineEndComment(int startPos) const; 0743 bool isBeforeMultipleLineEndComments(int startPos) const; 0744 bool isBraceType(BraceType a, BraceType b) const; 0745 bool isClassInitializer() const; 0746 bool isClosingHeader(const string* header) const; 0747 bool isCurrentBraceBroken() const; 0748 bool isDereferenceOrAddressOf() const; 0749 bool isExecSQL(const string& line, size_t index) const; 0750 bool isEmptyLine(const string& line) const; 0751 bool isExternC() const; 0752 bool isMultiStatementLine() const; 0753 bool isNextWordSharpNonParenHeader(int startChar) const; 0754 bool isNonInStatementArrayBrace() const; 0755 bool isNumericVariable(string word) const; 0756 bool isOkToSplitFormattedLine(); 0757 bool isPointerOrReference() const; 0758 bool isPointerOrReferenceCentered() const; 0759 bool isPointerOrReferenceVariable(const string& word) const; 0760 bool isPointerToPointer(const string& line, int currPos) const; 0761 bool isSharpStyleWithParen(const string* header) const; 0762 bool isStructAccessModified(const string& firstLine, size_t index) const; 0763 bool isIndentablePreprocessorBlock(const string& firstLine, size_t index); 0764 bool isNDefPreprocStatement(const string& nextLine_, const string& preproc) const; 0765 bool isUnaryOperator() const; 0766 bool isUniformInitializerBrace() const; 0767 bool isImmediatelyPostCast() const; 0768 bool isInExponent() const; 0769 bool isInSwitchStatement() const; 0770 bool isNextCharOpeningBrace(int startChar) const; 0771 bool isOkToBreakBlock(BraceType braceType) const; 0772 bool isOperatorPaddingDisabled() const; 0773 bool pointerSymbolFollows() const; 0774 int findObjCColonAlignment() const; 0775 int getCurrentLineCommentAdjustment(); 0776 int getNextLineCommentAdjustment(); 0777 int isOneLineBlockReached(const string& line, int startChar) const; 0778 void adjustComments(); 0779 void appendChar(char ch, bool canBreakLine); 0780 void appendCharInsideComments(); 0781 void appendClosingHeader(); 0782 void appendOperator(const string& sequence, bool canBreakLine = true); 0783 void appendSequence(const string& sequence, bool canBreakLine = true); 0784 void appendSpacePad(); 0785 void appendSpaceAfter(); 0786 void breakLine(bool isSplitLine = false); 0787 void buildLanguageVectors(); 0788 void updateFormattedLineSplitPoints(char appendedChar); 0789 void updateFormattedLineSplitPointsOperator(const string& sequence); 0790 void checkIfTemplateOpener(); 0791 void clearFormattedLineSplitPoints(); 0792 void convertTabToSpaces(); 0793 void deleteContainer(vector<BraceType>*& container); 0794 void findReturnTypeSplitPoint(const string& firstLine); 0795 void formatArrayRunIn(); 0796 void formatRunIn(); 0797 void formatArrayBraces(BraceType braceType, bool isOpeningArrayBrace); 0798 void formatClosingBrace(BraceType braceType); 0799 void formatCommentBody(); 0800 void formatCommentOpener(); 0801 void formatCommentCloser(); 0802 void formatLineCommentBody(); 0803 void formatLineCommentOpener(); 0804 void formatOpeningBrace(BraceType braceType); 0805 void formatQuoteBody(); 0806 void formatQuoteOpener(); 0807 void formatPointerOrReference(); 0808 void formatPointerOrReferenceCast(); 0809 void formatPointerOrReferenceToMiddle(); 0810 void formatPointerOrReferenceToName(); 0811 void formatPointerOrReferenceToType(); 0812 void fixOptionVariableConflicts(); 0813 void goForward(int i); 0814 void isLineBreakBeforeClosingHeader(); 0815 void initContainer(vector<BraceType>*& container, vector<BraceType>* value); 0816 void initNewLine(); 0817 void padObjCMethodColon(); 0818 void padObjCMethodPrefix(); 0819 void padObjCParamType(); 0820 void padObjCReturnType(); 0821 void padOperators(const string* newOperator); 0822 void padParens(); 0823 void processPreprocessor(); 0824 void resetEndOfStatement(); 0825 void setAttachClosingBraceMode(bool state); 0826 void stripCommentPrefix(); 0827 void testForTimeToSplitFormattedLine(); 0828 void trimContinuationLine(); 0829 void updateFormattedLineSplitPointsPointerOrReference(size_t index); 0830 size_t findFormattedLineSplitPoint() const; 0831 size_t findNextChar(const string& line, char searchChar, int searchStart = 0) const; 0832 const string* checkForHeaderFollowingComment(const string& firstLine) const; 0833 const string* getFollowingOperator() const; 0834 string getPreviousWord(const string& line, int currPos) const; 0835 string peekNextText(const string& firstLine, 0836 bool endOnEmptyLine = false, 0837 shared_ptr<ASPeekStream> streamArg = nullptr) const; 0838 0839 private: // variables 0840 int formatterFileType; 0841 vector<const string*>* headers; 0842 vector<const string*>* nonParenHeaders; 0843 vector<const string*>* preDefinitionHeaders; 0844 vector<const string*>* preCommandHeaders; 0845 vector<const string*>* operators; 0846 vector<const string*>* assignmentOperators; 0847 vector<const string*>* castOperators; 0848 vector<const pair<const string, const string>* >* indentableMacros; // for ASEnhancer 0849 0850 ASSourceIterator* sourceIterator; 0851 ASEnhancer* enhancer; 0852 0853 vector<const string*>* preBraceHeaderStack; 0854 vector<BraceType>* braceTypeStack; 0855 vector<int>* parenStack; 0856 vector<bool>* structStack; 0857 vector<bool>* questionMarkStack; 0858 0859 string currentLine; 0860 string formattedLine; 0861 string readyFormattedLine; 0862 string verbatimDelimiter; 0863 const string* currentHeader; 0864 char currentChar; 0865 char previousChar; 0866 char previousNonWSChar; 0867 char previousCommandChar; 0868 char quoteChar; 0869 streamoff preprocBlockEnd; 0870 int charNum; 0871 int runInIndentChars; 0872 int nextLineSpacePadNum; 0873 int objCColonAlign; 0874 int preprocBraceTypeStackSize; 0875 int spacePadNum; 0876 int tabIncrementIn; 0877 int templateDepth; 0878 int squareBracketCount; 0879 size_t checksumIn; 0880 size_t checksumOut; 0881 size_t currentLineFirstBraceNum; // first brace location on currentLine 0882 size_t formattedLineCommentNum; // comment location on formattedLine 0883 size_t leadingSpaces; 0884 size_t maxCodeLength; 0885 size_t methodAttachCharNum; 0886 size_t methodAttachLineNum; 0887 size_t methodBreakCharNum; 0888 size_t methodBreakLineNum; 0889 0890 // possible split points 0891 size_t maxSemi; // probably a 'for' statement 0892 size_t maxAndOr; // probably an 'if' statement 0893 size_t maxComma; 0894 size_t maxParen; 0895 size_t maxWhiteSpace; 0896 size_t maxSemiPending; 0897 size_t maxAndOrPending; 0898 size_t maxCommaPending; 0899 size_t maxParenPending; 0900 size_t maxWhiteSpacePending; 0901 0902 size_t previousReadyFormattedLineLength; 0903 FormatStyle formattingStyle; 0904 BraceMode braceFormatMode; 0905 BraceType previousBraceType; 0906 PointerAlign pointerAlignment; 0907 ReferenceAlign referenceAlignment; 0908 ObjCColonPad objCColonPadMode; 0909 LineEndFormat lineEnd; 0910 bool isVirgin; 0911 bool isInVirginLine; 0912 bool shouldPadCommas; 0913 bool shouldPadOperators; 0914 bool shouldPadParensOutside; 0915 bool shouldPadFirstParen; 0916 bool shouldPadParensInside; 0917 bool shouldPadHeader; 0918 bool shouldStripCommentPrefix; 0919 bool shouldUnPadParens; 0920 bool shouldConvertTabs; 0921 bool shouldIndentCol1Comments; 0922 bool shouldIndentPreprocBlock; 0923 bool shouldCloseTemplates; 0924 bool shouldAttachExternC; 0925 bool shouldAttachNamespace; 0926 bool shouldAttachClass; 0927 bool shouldAttachClosingWhile; 0928 bool shouldAttachInline; 0929 bool isInLineComment; 0930 bool isInComment; 0931 bool isInCommentStartLine; 0932 bool noTrimCommentContinuation; 0933 bool isInPreprocessor; 0934 bool isInPreprocessorBeautify; 0935 bool isInTemplate; 0936 bool doesLineStartComment; 0937 bool lineEndsInCommentOnly; 0938 bool lineIsCommentOnly; 0939 bool lineIsLineCommentOnly; 0940 bool lineIsEmpty; 0941 bool isImmediatelyPostCommentOnly; 0942 bool isImmediatelyPostEmptyLine; 0943 bool isInClassInitializer; 0944 bool isInQuote; 0945 bool isInVerbatimQuote; 0946 bool haveLineContinuationChar; 0947 bool isInQuoteContinuation; 0948 bool isHeaderInMultiStatementLine; 0949 bool isSpecialChar; 0950 bool isNonParenHeader; 0951 bool foundQuestionMark; 0952 bool foundPreDefinitionHeader; 0953 bool foundNamespaceHeader; 0954 bool foundClassHeader; 0955 bool foundStructHeader; 0956 bool foundInterfaceHeader; 0957 bool foundPreCommandHeader; 0958 bool foundPreCommandMacro; 0959 bool foundTrailingReturnType; 0960 bool foundCastOperator; 0961 bool isInLineBreak; 0962 bool endOfAsmReached; 0963 bool endOfCodeReached; 0964 bool lineCommentNoIndent; 0965 bool isFormattingModeOff; 0966 bool isInEnum; 0967 bool isInExecSQL; 0968 bool isInAsm; 0969 bool isInAsmOneLine; 0970 bool isInAsmBlock; 0971 bool isLineReady; 0972 bool elseHeaderFollowsComments; 0973 bool caseHeaderFollowsComments; 0974 bool isPreviousBraceBlockRelated; 0975 bool isInPotentialCalculation; 0976 bool isCharImmediatelyPostComment; 0977 bool isPreviousCharPostComment; 0978 bool isCharImmediatelyPostLineComment; 0979 bool isCharImmediatelyPostOpenBlock; 0980 bool isCharImmediatelyPostCloseBlock; 0981 bool isCharImmediatelyPostTemplate; 0982 bool isCharImmediatelyPostReturn; 0983 bool isCharImmediatelyPostThrow; 0984 bool isCharImmediatelyPostNewDelete; 0985 bool isCharImmediatelyPostOperator; 0986 bool isCharImmediatelyPostPointerOrReference; 0987 bool isInObjCMethodDefinition; 0988 bool isInObjCInterface; 0989 bool isInObjCReturnType; 0990 bool isInObjCParam; 0991 bool isInObjCSelector; 0992 bool breakCurrentOneLineBlock; 0993 bool shouldRemoveNextClosingBrace; 0994 bool isInBraceRunIn; 0995 bool returnTypeChecked; 0996 bool currentLineBeginsWithBrace; 0997 bool attachClosingBraceMode; 0998 bool shouldBreakOneLineBlocks; 0999 bool shouldBreakOneLineHeaders; 1000 bool shouldBreakOneLineStatements; 1001 bool shouldBreakClosingHeaderBraces; 1002 bool shouldBreakElseIfs; 1003 bool shouldBreakLineAfterLogical; 1004 bool shouldAddBraces; 1005 bool shouldAddOneLineBraces; 1006 bool shouldRemoveBraces; 1007 bool shouldPadMethodColon; 1008 bool shouldPadMethodPrefix; 1009 bool shouldReparseCurrentChar; 1010 bool shouldUnPadMethodPrefix; 1011 bool shouldPadReturnType; 1012 bool shouldUnPadReturnType; 1013 bool shouldPadParamType; 1014 bool shouldUnPadParamType; 1015 bool shouldDeleteEmptyLines; 1016 bool shouldBreakReturnType; 1017 bool shouldBreakReturnTypeDecl; 1018 bool shouldAttachReturnType; 1019 bool shouldAttachReturnTypeDecl; 1020 bool needHeaderOpeningBrace; 1021 bool shouldBreakLineAtNextChar; 1022 bool shouldKeepLineUnbroken; 1023 bool passedSemicolon; 1024 bool passedColon; 1025 bool isImmediatelyPostNonInStmt; 1026 bool isCharImmediatelyPostNonInStmt; 1027 bool isImmediatelyPostComment; 1028 bool isImmediatelyPostLineComment; 1029 bool isImmediatelyPostEmptyBlock; 1030 bool isImmediatelyPostObjCMethodPrefix; 1031 bool isImmediatelyPostPreprocessor; 1032 bool isImmediatelyPostReturn; 1033 bool isImmediatelyPostThrow; 1034 bool isImmediatelyPostNewDelete; 1035 bool isImmediatelyPostOperator; 1036 bool isImmediatelyPostTemplate; 1037 bool isImmediatelyPostPointerOrReference; 1038 bool shouldBreakBlocks; 1039 bool shouldBreakClosingHeaderBlocks; 1040 bool isPrependPostBlockEmptyLineRequested; 1041 bool isAppendPostBlockEmptyLineRequested; 1042 bool isIndentableProprocessor; 1043 bool isIndentableProprocessorBlock; 1044 bool prependEmptyLine; 1045 bool appendOpeningBrace; 1046 bool foundClosingHeader; 1047 bool isInHeader; 1048 bool isImmediatelyPostHeader; 1049 bool isInCase; 1050 bool isFirstPreprocConditional; 1051 bool processedFirstConditional; 1052 bool isJavaStaticConstructor; 1053 1054 private: // inline functions 1055 // append the CURRENT character (curentChar) to the current formatted line. 1056 void appendCurrentChar(bool canBreakLine = true) 1057 { appendChar(currentChar, canBreakLine); } 1058 1059 // check if a specific sequence exists in the current placement of the current line 1060 bool isSequenceReached(const char* sequence) const 1061 { return currentLine.compare(charNum, strlen(sequence), sequence) == 0; } 1062 1063 // call ASBase::findHeader for the current character 1064 const string* findHeader(const vector<const string*>* headers_) 1065 { return ASBase::findHeader(currentLine, charNum, headers_); } 1066 1067 // call ASBase::findOperator for the current character 1068 const string* findOperator(const vector<const string*>* operators_) 1069 { return ASBase::findOperator(currentLine, charNum, operators_); } 1070 }; // Class ASFormatter 1071 1072 //----------------------------------------------------------------------------- 1073 // astyle namespace global declarations 1074 //----------------------------------------------------------------------------- 1075 // sort comparison functions for ASResource 1076 bool sortOnLength(const string* a, const string* b); 1077 bool sortOnName(const string* a, const string* b); 1078 1079 } // namespace astyle 1080 1081 // end of astyle namespace -------------------------------------------------- 1082 1083 #endif // closes ASTYLE_H