Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

No include guard indent #69

Merged
merged 3 commits into from

2 participants

@rdan

Hi Ben,

Here is an update of the tool mainly to apply "#include guard" strategy to code.
I have not created an option as it seems that this update has no impact on tests.
But if you prefer I add an option, don't hesitate to ask me.

Specific actions were taken for preprocessor directives.
It is now extended to all code:

  • code inside the "#include guard" is not indented
  • optimization of "ifdef_over_whole_file" to be able to call it several times
  • Update of the comment of some options to clarify their actions

I merged your last updates in this branch but it wa not really intentional (test of a command that didn't do what I expected to) => I have not tested this merge.

This replace pull request #67 that I closed (code not pushed correctly from my local repository and the github repository).

Ronan

rdan added some commits
@rdan rdan - Update of the management of the #include guard
  Specific actions were taken for preprocessor directives.
  It is now extended to all code:
    - code inside the "#include guard" is not indented
    - optimization of "ifdef_over_whole_file" to be able to call it several times
- Update of the comment of some options to clarify their actions
a4b6730
@rdan rdan Tests updated to validate the modifications in this branch 7df80a1
@rdan rdan Merge branch 'master' of git://github.com/bengardner/uncrustify into …
…no_include_guard_indent
86ca62b
@bengardner

I doubt this is correct. What happens when multiple files are processed in one process?

@bengardner bengardner merged commit 86ca62b into bengardner:master
@bengardner
Owner

I merged it with a little bit of cleanup. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2012
  1. @rdan

    - Update of the management of the #include guard

    rdan authored
      Specific actions were taken for preprocessor directives.
      It is now extended to all code:
        - code inside the "#include guard" is not indented
        - optimization of "ifdef_over_whole_file" to be able to call it several times
    - Update of the comment of some options to clarify their actions
Commits on May 12, 2012
  1. @rdan
  2. @rdan

    Merge branch 'master' of git://github.com/bengardner/uncrustify into …

    rdan authored
    …no_include_guard_indent
This page is out of date. Refresh to see the latest.
View
29 src/indent.cpp
@@ -117,6 +117,7 @@
*/
static void indent_comment(chunk_t *pc, int col);
+static bool ifdef_over_whole_file();
void indent_to_column(chunk_t *pc, int column)
@@ -400,10 +401,17 @@ void indent_text(void)
int sql_orig_col = 0;
bool in_func_def = false;
c_token_t memtype;
+ bool has_include_guard = false;
memset(&frm, 0, sizeof(frm));
cpd.frame_count = 0;
+ /* Check if file contains a #include guard */
+ if (ifdef_over_whole_file())
+ {
+ has_include_guard = true;
+ }
+
/* dummy top-level entry */
frm.pse[0].indent = 1;
frm.pse[0].indent_tmp = 1;
@@ -510,9 +518,18 @@ void indent_text(void)
indent_pse_push(frm, next);
next->type = memtype;
- /* Indent one level */
- frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent + indent_size;
- frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab + indent_size;
+ /* Indent one level except if the #if is a #include guard */
+ if (has_include_guard &&
+ (pc->pp_level == 0))
+ {
+ frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent;
+ frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab;
+ }
+ else
+ {
+ frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent + indent_size;
+ frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab + indent_size;
+ }
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos].in_preproc = false;
}
@@ -1701,8 +1718,12 @@ static bool ifdef_over_whole_file()
chunk_t *pc;
chunk_t *next;
- int stage = 0;
+ static int stage = -1;
+
+ if (stage > 0)
+ return(stage == 2);
+ stage = 0;
for (pc = chunk_get_head(); pc != NULL; pc = chunk_get_next(pc))
{
if (chunk_is_comment(pc) || chunk_is_newline(pc))
View
14 src/options.cpp
@@ -1050,7 +1050,7 @@ void register_options(void)
unc_add_option("align_pp_define_gap", UO_align_pp_define_gap, AT_NUM,
"The minimum space between label and value of a preprocessor define");
unc_add_option("align_pp_define_span", UO_align_pp_define_span, AT_NUM,
- "The span for aligning on '#define' bodies (0=don't align)", "", 0, 5000);
+ "The span for aligning on '#define' bodies (0=don't align, other=number of lines including comments between blocks)", "", 0, 5000);
unc_add_option("align_left_shift", UO_align_left_shift, AT_BOOL,
"Align lines that start with '<<' with previous '<<'. Default=true");
@@ -1171,11 +1171,13 @@ void register_options(void)
unc_begin_group(UG_preprocessor, "Preprocessor options");
unc_add_option("pp_indent", UO_pp_indent, AT_IARF,
- "Control indent of preprocessors inside #if blocks at brace level 0");
+ "Control indent of preprocessors inside #if blocks at brace level 0 (file-level)");
unc_add_option("pp_indent_at_level", UO_pp_indent_at_level, AT_BOOL,
"Whether to indent #if/#else/#endif at the brace level (true) or from column 1 (false)");
unc_add_option("pp_indent_count", UO_pp_indent_count, AT_NUM,
- "If pp_indent_at_level=false, specifies the number of columns to indent per level. Default=1.");
+ "Specifies the number of columns to indent preprocessors per level at brace level 0 (file-level).\n"
+ "If pp_indent_at_level=false, specifies the number of columns to indent preprocessors per level at brace level > 0 (function-level).\n"
+ "Default=1.");
unc_add_option("pp_space", UO_pp_space, AT_IARF,
"Add or remove space after # based on pp_level of #if blocks");
unc_add_option("pp_space_count", UO_pp_space_count, AT_NUM,
@@ -1185,9 +1187,11 @@ void register_options(void)
unc_add_option("pp_region_indent_code", UO_pp_region_indent_code, AT_BOOL,
"Whether to indent the code between #region and #endregion");
unc_add_option("pp_indent_if", UO_pp_indent_if, AT_NUM,
- "If pp_indent_at_level=true, sets the indent for #if, #else, and #endif when not at file-level");
+ "If pp_indent_at_level=true, sets the indent for #if, #else, and #endif when not at file-level.\n"
+ "0: indent preprocessors using output_tab_size.\n"
+ ">0: column at which all preprocessors will be indented.");
unc_add_option("pp_if_indent_code", UO_pp_if_indent_code, AT_BOOL,
- "Control whether to indent the code between #if, #else and #endif when not at file-level");
+ "Control whether to indent the code between #if, #else and #endif.");
unc_add_option("pp_define_at_level", UO_pp_define_at_level, AT_BOOL,
"Whether to indent '#define' at the brace level (true) or from column 1 (false)");
}
View
1  tests/c.test
@@ -156,6 +156,7 @@
00615 pp-indent-2.cfg c/pp-nest.c
00616 pp_if_indent-1.cfg c/pp-if-indent.c
00617 rdan.cfg c/pp-if-indent.c
+00618 pp_if_indent-4.cfg c/pp-if-indent.c
00620 ben.cfg c/indent-assign.c
00621 nl_endif.cfg c/nl_endif.c
View
10 tests/config/pp_if_indent-4.cfg
@@ -0,0 +1,10 @@
+# indented region code
+pp_indent = add
+pp_indent_count = 1
+indent_columns = 4
+pp_if_indent_code = true
+pp_indent_if = 0
+pp_indent_at_level = true
+pp_space = remove
+pp_space_count = 0
+pp_define_at_level = true
View
50 tests/input/c/pp-if-indent.c
@@ -1,4 +1,23 @@
-/* Test rules chapter 6.8 */
+/* this is a comment */
+
+/* should be in a H file but put in this file to avoid multiplying the number of test files */
+#ifndef COMSTACK_TYPES_H
+#define COMSTACK_TYPES_H
+
+#include "STD_TYPES.h"
+#ifdef COMINL_coENABLE_1
+#ifdef COMINL_coENABLE_2
+#include "def.h"
+#endif
+#endif
+
+#if (COMINL_coMINIMUM_DELAY_TIME_1 == COMINL_coENABLE)
+#if (COMINL_coMINIMUM_DELAY_TIME_2 == COMINL_coENABLE)
+#include "def1.h"
+#define COMINL_coMINIMUM_DELAY_TIME_1
+#include "def2.h"
+#endif
+#endif
/* no indentation */
#if COMINL_coMINIMUM_DELAY_TIME == COMINL_coENABLE
@@ -16,6 +35,19 @@
#endif
+#ifdef COMINL_coENABLE_3
+typedef enum
+{
+ BUFREQ_OK =0,
+ BUFREQ_E_NOT_OK = 1,
+ BUFREQ_E_BUSY=2,
+BUFREQ_E_OVFL =3
+}
+BufReq_ReturnType;
+#endif
+
+
+
#if COMINL_coSTART_STOP_PERIODIC == COMINL_coENABLE
void COMINL_vidInit(void)
{
@@ -68,4 +100,20 @@ void COMINL_vidInit(void)
}
#endif
+
+void myfunction(void)
+{
+int i;
+#ifdef COMINL_coTX_MESSAGE_VAR
+#ifndef COMINL_coMIXED_MODE
+#pragma MyPragma
+int j;
+#endif
+#endif
+int k;
+}
+
+
+#endif /* COMSTACK_TYPES_H */
+
/*------------------------------- end of file --------------------------------*/
View
50 tests/output/c/00616-pp-if-indent.c
@@ -1,4 +1,23 @@
-/* Test rules chapter 6.8 */
+/* this is a comment */
+
+/* should be in a H file but put in this file to avoid multiplying the number of test files */
+#ifndef COMSTACK_TYPES_H
+#define COMSTACK_TYPES_H
+
+#include "STD_TYPES.h"
+#ifdef COMINL_coENABLE_1
+ #ifdef COMINL_coENABLE_2
+ #include "def.h"
+ #endif
+#endif
+
+#if (COMINL_coMINIMUM_DELAY_TIME_1 == COMINL_coENABLE)
+ #if (COMINL_coMINIMUM_DELAY_TIME_2 == COMINL_coENABLE)
+ #include "def1.h"
+ #define COMINL_coMINIMUM_DELAY_TIME_1
+ #include "def2.h"
+ #endif
+#endif
/* no indentation */
#if COMINL_coMINIMUM_DELAY_TIME == COMINL_coENABLE
@@ -16,6 +35,19 @@
#endif
+#ifdef COMINL_coENABLE_3
+ typedef enum
+ {
+ BUFREQ_OK =0,
+ BUFREQ_E_NOT_OK = 1,
+ BUFREQ_E_BUSY=2,
+ BUFREQ_E_OVFL =3
+ }
+ BufReq_ReturnType;
+#endif
+
+
+
#if COMINL_coSTART_STOP_PERIODIC == COMINL_coENABLE
void COMINL_vidInit(void)
{
@@ -68,4 +100,20 @@
}
#endif
+
+void myfunction(void)
+{
+ int i;
+ #ifdef COMINL_coTX_MESSAGE_VAR
+ #ifndef COMINL_coMIXED_MODE
+ #pragma MyPragma
+ int j;
+ #endif
+ #endif
+ int k;
+}
+
+
+#endif /* COMSTACK_TYPES_H */
+
/*------------------------------- end of file --------------------------------*/
View
49 tests/output/c/00617-pp-if-indent.c
@@ -1,4 +1,24 @@
-/* Test rules chapter 6.8 */
+/* this is a comment */
+
+/* should be in a H file but put in this file to avoid multiplying the number of
+ *test files */
+#ifndef COMSTACK_TYPES_H
+#define COMSTACK_TYPES_H
+
+#include "STD_TYPES.h"
+#ifdef COMINL_coENABLE_1
+ #ifdef COMINL_coENABLE_2
+ #include "def.h"
+ #endif
+#endif
+
+#if (COMINL_coMINIMUM_DELAY_TIME_1 == COMINL_coENABLE)
+ #if (COMINL_coMINIMUM_DELAY_TIME_2 == COMINL_coENABLE)
+ #include "def1.h"
+ #define COMINL_coMINIMUM_DELAY_TIME_1
+ #include "def2.h"
+ #endif
+#endif
/* no indentation */
#if COMINL_coMINIMUM_DELAY_TIME == COMINL_coENABLE
@@ -15,6 +35,17 @@
#error "Define COMINL_coENABLE is undefined"
#endif
+#ifdef COMINL_coENABLE_3
+ typedef enum
+ {
+ BUFREQ_OK = 0,
+ BUFREQ_E_NOT_OK = 1,
+ BUFREQ_E_BUSY = 2,
+ BUFREQ_E_OVFL = 3
+ }
+ BufReq_ReturnType;
+#endif
+
#if COMINL_coSTART_STOP_PERIODIC == COMINL_coENABLE
void COMINL_vidInit(void)
{
@@ -74,4 +105,20 @@ void COMINL_vidInit(void)
#endif
+void myfunction(void)
+{
+ int i;
+
+
+ #ifdef COMINL_coTX_MESSAGE_VAR
+ #ifndef COMINL_coMIXED_MODE
+ #pragma MyPragma
+ int j;
+ #endif
+ #endif
+ int k;
+}
+
+#endif /* COMSTACK_TYPES_H */
+
/*------------------------------- end of file --------------------------------*/
View
119 tests/output/c/00618-pp-if-indent.c
@@ -0,0 +1,119 @@
+/* this is a comment */
+
+/* should be in a H file but put in this file to avoid multiplying the number of test files */
+#ifndef COMSTACK_TYPES_H
+#define COMSTACK_TYPES_H
+
+#include "STD_TYPES.h"
+#ifdef COMINL_coENABLE_1
+ #ifdef COMINL_coENABLE_2
+ #include "def.h"
+ #endif
+#endif
+
+#if (COMINL_coMINIMUM_DELAY_TIME_1 == COMINL_coENABLE)
+ #if (COMINL_coMINIMUM_DELAY_TIME_2 == COMINL_coENABLE)
+ #include "def1.h"
+ #define COMINL_coMINIMUM_DELAY_TIME_1
+ #include "def2.h"
+ #endif
+#endif
+
+/* no indentation */
+#if COMINL_coMINIMUM_DELAY_TIME == COMINL_coENABLE
+ #include "MEMSRV.h"
+#endif
+
+/* already well indented */
+#ifndef COMINL_coAPPLI_TX_CONFIRMATION
+ #error "Define COMINL_coAPPLI_TX_CONFIRMATION is undefined"
+#endif
+
+/* bad indentation */
+#ifndef COMINL_coENABLE
+ #error "Define COMINL_coENABLE is undefined"
+#endif
+
+
+#ifdef COMINL_coENABLE_3
+ typedef enum
+ {
+ BUFREQ_OK =0,
+ BUFREQ_E_NOT_OK = 1,
+ BUFREQ_E_BUSY=2,
+ BUFREQ_E_OVFL =3
+ }
+ BufReq_ReturnType;
+#endif
+
+
+
+#if COMINL_coSTART_STOP_PERIODIC == COMINL_coENABLE
+ void COMINL_vidInit(void)
+ {
+ uint8 u8LocalMsgIdx;
+
+ if (COMINL_kastrVarReceiveMsg[u8LocalMsgIdx].udtDeadlineMonTimer < COMINL_udtNB_MESSAGES_RX)
+ {
+ u8LocalMsgIdx = E_OK;
+ }
+ else
+ {
+ u8LocalMsgIdx = E_NOT_OK;
+ }
+
+/* nested #if...already well indented */
+ #if COMINL_coRX_MESSAGE_VAR == COMINL_coENABLE
+ /*!Trace to: VEES_R_11_04044_004.01*/
+ for(u8LocalMsgIdx = 0; u8LocalMsgIdx < COMINL_udtNB_MESSAGES_RX; u8LocalMsgIdx++)
+ {
+ #if COMINL_coRX_DEADLINE_MONITORING == COMINL_coENABLE
+ COMINL_kastrVarReceiveMsg[u8LocalMsgIdx].udtDeadlineMonTimer = 0;
+ #else /* COMINL_coRX_DEADLINE_MONITORING == COMINL_coENABLE */
+ COMINL_kastrVarReceiveMsg[u8LocalMsgIdx].udtINMDeadlineMonTimer = 0;
+ #endif /* COMINL_coRX_DEADLINE_MONITORING == COMINL_coENABLE */
+ }
+ #endif /* COMINL_coRX_MESSAGE_VAR == COMINL_coENABLE */
+
+
+
+/* nested #if... no indentation */
+ #if COMINL_coTX_MESSAGE_VAR == COMINL_coENABLE
+ /*!Trace to: VEES_R_11_04044_004.01*/
+ for(u8LocalMsgIdx = 0; u8LocalMsgIdx < COMINL_udtNB_MESSAGES_TX; u8LocalMsgIdx++)
+ {
+ #if COMINL_coTX_DEADLINE_MONITORING == COMINL_coENABLE
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].udtDeadlineMonTimer = 0;
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].bDeadlineMonEnable = FALSE;
+ #endif /* COMINL_coTX_DEADLINE_MONITORING == COMINL_coENABLE */
+ #if COMINL_coTX_INM_DEADLINE_MONITORING == COMINL_coENABLE
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].udtINMDeadlineMonTimer = 0;
+ #else /* COMINL_coTX_INM_DEADLINE_MONITORING == COMINL_coENABLE */
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].udtMDTTimer = 0;
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].bMDTMsgToSend = FALSE;
+ #endif /*COMINL_coMINIMUM_DELAY_TIME == COMINL_coENABLE*/
+ #if COMINL_coMIXED_MODE != COMINL_coDISABLE
+ COMINL_kastrVarTransmitMsg[u8LocalMsgIdx].udtPeriodicTimer = 0;
+ #endif
+ }
+ #endif /* COMINL_coTX_MESSAGE_VAR == COMINL_coENABLE */
+ }
+#endif
+
+
+void myfunction(void)
+{
+ int i;
+ #ifdef COMINL_coTX_MESSAGE_VAR
+ #ifndef COMINL_coMIXED_MODE
+ #pragma MyPragma
+ int j;
+ #endif
+ #endif
+ int k;
+}
+
+
+#endif /* COMSTACK_TYPES_H */
+
+/*------------------------------- end of file --------------------------------*/
View
20 win32/Uncrustify_Win32_2010.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Uncrustify", "Uncrustify_Win32_2010.vcxproj", "{743E549D-6BCA-4E81-8DCF-C34B85AF8373}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {743E549D-6BCA-4E81-8DCF-C34B85AF8373}.Debug|Win32.ActiveCfg = Debug|Win32
+ {743E549D-6BCA-4E81-8DCF-C34B85AF8373}.Debug|Win32.Build.0 = Debug|Win32
+ {743E549D-6BCA-4E81-8DCF-C34B85AF8373}.Release|Win32.ActiveCfg = Release|Win32
+ {743E549D-6BCA-4E81-8DCF-C34B85AF8373}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
Something went wrong with that request. Please try again.