Format C++ code intelligently in Visual Studio with clang-format

Two month ago in GoingNative 2013 Chandler Carruth demonstrated a new tool clang-format to automatically format C++ code. The whole talk is excellent, but if you just interested in formatting watch between 24 and 32 minutes. For Visual Studio users there is now a LLVM Windows snapshot build available and the clang-format plugin for Visual Studio, see LLVM Snapshot Builds.

To demonstrate how clang-format works, on a Visual Studio 2012 machine I installed the LLVM toolchain, clang-format plugin and created a default MFC application. Below are some examples how clang-format improved the default generated code:

Original:
CPropertiesWnd::CPropertiesWnd()
{
	m_nComboHeight = 0;
}

CPropertiesWnd::~CPropertiesWnd()
{
}
clang-format:
CPropertiesWnd::CPropertiesWnd() { m_nComboHeight = 0; }

CPropertiesWnd::~CPropertiesWnd() {}
Original:
void CPropertiesWnd::AdjustLayout()
{
	if (GetSafeHwnd () == NULL || (AfxGetMainWnd() != NULL && AfxGetMainWnd()->IsIconic()))
	{
		return;
	}

	CRect rectClient;
	GetClientRect(rectClient);

	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

	m_wndObjectCombo.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), m_nComboHeight, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top + m_nComboHeight, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndPropList.SetWindowPos(NULL, rectClient.left, rectClient.top + m_nComboHeight + cyTlb, rectClient.Width(), rectClient.Height() -(m_nComboHeight+cyTlb), SWP_NOACTIVATE | SWP_NOZORDER);
}
clang-format:
void CPropertiesWnd::AdjustLayout() {
  if (GetSafeHwnd() == NULL ||
      (AfxGetMainWnd() != NULL && AfxGetMainWnd()->IsIconic())) {
    return;
  }

  CRect rectClient;
  GetClientRect(rectClient);

  int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

  m_wndObjectCombo.SetWindowPos(NULL, rectClient.left, rectClient.top,
                                rectClient.Width(), m_nComboHeight,
                                SWP_NOACTIVATE | SWP_NOZORDER);
  m_wndToolBar.SetWindowPos(NULL, rectClient.left,
                            rectClient.top + m_nComboHeight, rectClient.Width(),
                            cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
  m_wndPropList.SetWindowPos(
      NULL, rectClient.left, rectClient.top + m_nComboHeight + cyTlb,
      rectClient.Width(), rectClient.Height() - (m_nComboHeight + cyTlb),
      SWP_NOACTIVATE | SWP_NOZORDER);
}
Original:
m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
clang-format:
  m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() &
                            ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC |
                              CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM |
                              CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));

clang-format is also highly customizable for your particular coding style, see Clang-Format Style Options. You can create a .clang-format or _clang-format file with options like IndentWidth: 4, BreakBeforeBraces: Allman and place this file to the source file directory or one of its parent directories.

The Tools.ClangFormat command from the extensions formats the cursor line when no text is selected or the selected lines. In my experience it works best when the whole document is selected. I want to investigate how to format the whole document without manually selecting it first. I also want to run formatting in other Visual Studio versions, primarily in Visual Studio 2008, Visual Studio 2010 and Visual Studio 2013.

If you use Visual Studio 2012, you can try clang-format right now.

Update: The corresponding Reddit discussion thread for this article describes how to run the ClangFormat extension in Visual Studio 2013 if you don’t want to wait for an official update.

Update 2 (November 27, 2013): Manuel Klimek has added support for VS 2010 and VS 2013 to the ClangFormat extension. You can download it from the official page.

 

Organize Visual Studio tabs with Tabs Studio add-in

This entry was posted in Reviews and tagged , , , . Bookmark the permalink.

2 Responses to Format C++ code intelligently in Visual Studio with clang-format

  1. Ionut Enache says:

    7 years later if you want an easy way to use clang combined with Visual Studio you can check Clang Power Tools VS-extension. For those who need it, here is the link: https://www.clangpowertools.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s