Tryer
Tryer

Reputation: 4080

clang-format does not modify file on disk

I have the following badly formatted file, test.cpp

#include "maininclude.h"

int main() {
             class SIMPLE smpl;
  for (int i = 1; i < 100; 
  i++) {
      printf("Printing %d", i); // Trying out different stuff...
getchar();
  }
}

on which I want to run clang-format. Prior to running this, to visually see the places that are going to be modified, I run clang-format -n test.cpp. This correctly identifies the spots that will be changed due to bad formatting and outputs on the terminal:

test.cpp:3:13: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {
            ^
test.cpp:5:27: warning: code should be clang-formatted [-Wclang-format-violations]
  for (int i = 1; i < 100; 
                          ^
test.cpp:6:9: warning: code should be clang-formatted [-Wclang-format-violations]
  i++) {
        ^
test.cpp:7:64: warning: code should be clang-formatted [-Wclang-format-violations]
          printf("Printing %d", i); // Trying out different stuff...
                                                                    ^

On running clang-format test.cpp, I obtain how the correctly formatted file would look like (this display is on the terminal):

#include "maininclude.h"

int main() {
  class SIMPLE smpl;
  for (int i = 1; i < 100; i++) {
    printf("Printing %d", i); // Trying out different stuff...
    getchar();
  }
}

Yet, running the above command does not change the actual test.cpp file on disk. Is there a separate option/command to have clang-format go ahead and apply its changes and save the file on disk?

Upvotes: 3

Views: 3631

Answers (2)

alfC
alfC

Reputation: 16270

From man clang-format:

       -i                         - Inplace edit <file>s, if specified.

Upvotes: 8

Luis Guzman
Luis Guzman

Reputation: 1026

Use the -i option, which means Inplace edit <file>s. It will edit the file in place.

Upvotes: 2

Related Questions