Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit caf2cee

Browse files
committed
Don't warn on #pragma once in header files
If the file being linted has an extension of .h, .hpp, or .hh enable the compiler option to ignore #pragma once in the "main file", since it is perfectly valid here and this is considered the main file.
1 parent c815733 commit caf2cee

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

lib/main.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
44
import { CompositeDisposable } from 'atom';
5-
import { dirname } from 'path';
5+
import { dirname, extname } from 'path';
66

77
let helpers = null;
88
let clangFlags = null;
@@ -73,6 +73,7 @@ export default {
7373
}
7474

7575
const filePath = editor.getPath();
76+
const fileExt = extname(filePath);
7677
const fileText = editor.getText();
7778

7879
const args = [
@@ -87,6 +88,10 @@ export default {
8788
const grammar = editor.getGrammar().name;
8889

8990
switch (grammar) {
91+
case 'Objective-C':
92+
args.push('-xobjective-c');
93+
args.push(...this.clangDefaultObjCFlags.split(/\s+/));
94+
break;
9095
case 'Objective-C++':
9196
args.push('-xobjective-c++');
9297
args.push(...this.clangDefaultObjCppFlags.split(/\s+/));
@@ -95,10 +100,6 @@ export default {
95100
args.push('-xc');
96101
args.push(...this.clangDefaultCFlags.split(/\s+/));
97102
break;
98-
case 'Objective-C':
99-
args.push('-xobjective-c');
100-
args.push(...this.clangDefaultObjCFlags.split(/\s+/));
101-
break;
102103
default:
103104
case 'C++':
104105
case 'C++14':
@@ -107,6 +108,11 @@ export default {
107108
break;
108109
}
109110

111+
if (fileExt === '.hpp' || fileExt === '.hh' || fileExt === '.h') {
112+
// Don't warn about #pragma once when linting header files
113+
args.push('-Wno-pragma-once-outside-header');
114+
}
115+
110116
if (this.clangSuppressWarnings) {
111117
args.push('-w');
112118
}

spec/files/pragma/pragma_once.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int main() {}

spec/files/pragma/pragma_once.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int main() {}

spec/files/pragma/pragma_once.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int main() {}

spec/files/pragma/pragma_once.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int main() {}

spec/linter-clang-spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { join } from 'path';
77
const lint = require('../lib/main').provideLinter().lint;
88

99
const miPath = join(__dirname, 'files', 'missing_import');
10+
const poPath = join(__dirname, 'files', 'pragma', 'pragma_once');
1011
const validPath = join(__dirname, 'files', 'valid.c');
1112

1213
describe('The Clang provider for AtomLinter', () => {
@@ -62,4 +63,38 @@ describe('The Clang provider for AtomLinter', () => {
6263
expect(messages[0].location.position).toEqual([[1, 9], [1, 20]]);
6364
});
6465
});
66+
67+
describe('handles pragma once properly', () => {
68+
it('finds a pragma once warning in pragma_once.c', async () => {
69+
const editor = await atom.workspace.open(`${poPath}.c`);
70+
const messages = await lint(editor);
71+
expect(messages.length).toEqual(1);
72+
expect(messages[0].severity).toEqual('warning');
73+
expect(messages[0].excerpt).toEqual('#pragma once in main file [-Wpragma-once-outside-header]');
74+
expect(messages[0].location.file).toBe(`${poPath}.c`);
75+
expect(messages[0].location.position).toEqual([[0, 8], [0, 12]]);
76+
});
77+
78+
it('finds a pragma once warning in pragma_once.cpp', async () => {
79+
const editor = await atom.workspace.open(`${poPath}.cpp`);
80+
const messages = await lint(editor);
81+
expect(messages.length).toEqual(1);
82+
expect(messages[0].severity).toEqual('warning');
83+
expect(messages[0].excerpt).toEqual('#pragma once in main file [-Wpragma-once-outside-header]');
84+
expect(messages[0].location.file).toBe(`${poPath}.cpp`);
85+
expect(messages[0].location.position).toEqual([[0, 8], [0, 12]]);
86+
});
87+
88+
it("doesn't find a pragma once warning in pragma_once.h", async () => {
89+
const editor = await atom.workspace.open(`${poPath}.h`);
90+
const messages = await lint(editor);
91+
expect(messages.length).toEqual(0);
92+
});
93+
94+
it("doesn't find a pragma once warning in pragma_once.hpp", async () => {
95+
const editor = await atom.workspace.open(`${poPath}.hpp`);
96+
const messages = await lint(editor);
97+
expect(messages.length).toEqual(0);
98+
});
99+
});
65100
});

0 commit comments

Comments
 (0)