Skip to content

Commit

Permalink
Add preferences to handle clang-tidy functionality when clangd v9+ is…
Browse files Browse the repository at this point in the history
… used.

Fixes issue #4579
This is a new linter for C/C++. There are two ways to initiate the linter:
  - From the preferences
  - Adding a new file located in the same folder of the files or a parent folder: .clang-tidy

Signed-off-by: Jacques Bouthillier <jacques.bouthillier@ericsson.com>
  • Loading branch information
lmcbout committed Mar 18, 2019
1 parent a2f831c commit e94260d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Change Log

## v0.5.0

- [cpp] added new clang-tidy and clang-tidy-checks preferences to lint cpp program when clangd v9+ is used.
- [plugin] `workspace.openTextDocument` API now respects the contributed `FileSystemProviders`

Breaking changes:

- [editor] computation of resource context keys moved to core [#4531](https://github.com/theia-ide/theia/pull/4531)
- [plugin] support multiple windows per a backend [#4509](https://github.com/theia-ide/theia/issues/4509)
- Some plugin bindings are scoped per a connection now. Clients, who contribute/rebind these bindings, will need to scope them per a connection as well.
- [quick-open] disable separate fuzzy matching by default [#4549](https://github.com/theia-ide/theia/pull/4549)

## v0.4.0

- [application-manager] added support for pre-load HTML templates
- [console] added support for console `when` contexts
- [core] added support for os `when` contexts
Expand Down Expand Up @@ -86,6 +90,7 @@ Breaking changes:


Breaking changes:

- menus aligned with built-in VS Code menus [#4173](https://github.com/theia-ide/theia/pull/4173)
- navigator context menu group changes:
- `1_open` and `4_new` replaced by `navigation` group
Expand Down Expand Up @@ -123,6 +128,7 @@ Breaking changes:


## v0.3.19

- [core] added `hostname` alias
- [core] added new `editor.formatOnSave` preference, to format documents on manual save
- [core] added support for setting end of line character
Expand Down Expand Up @@ -164,6 +170,7 @@ Breaking changes:


## v0.3.18

- [core] added a preference to define how to handle application exit
- [core] added a way to prevent application exit from extensions
- [core] added functionality to prevent application exit if some editors are dirty
Expand Down Expand Up @@ -203,6 +210,7 @@ Breaking changes:


## v0.3.17

- Added better widget error handling for different use cases (ex: no workspace present, no repository present, ...)
- Addressed multiple backend memory leaks
- Prefixed quick-open commands for easier categorization and searching
Expand Down Expand Up @@ -241,6 +249,7 @@ Breaking changes:


## v0.3.16

- Reverted [cpp] Add debugging for C/C++ programs. This feature will come back in its own cpp-specific repo
- [callhierarchy][typescript] adapt to hierarchical document symbols
- [core] added methods to un-register menus, commands and keybindings
Expand All @@ -254,6 +263,7 @@ Breaking changes:


## v0.3.15

- [cpp] added debugging for C/C++ programs
- [debug] added debug toolbar
- [debug] resolved variables in configurations
Expand All @@ -265,6 +275,7 @@ Breaking changes:


## v0.3.13

- Re-implemented additional widgets using React
- Re-implemented miscellaneous components using React
- [cpp] added a status bar button to select an active cpp build configuration
Expand All @@ -280,6 +291,7 @@ Breaking changes:


## v0.3.12

- New Plugin system !
- See [design](https://github.com/theia-ide/theia/issues/1482) and [documentation](https://github.com/theia-ide/theia/blob/master/packages/plugin/API.md) for more details.
- Introducing [Task API](https://github.com/theia-ide/theia/pull/2086).
Expand All @@ -299,6 +311,7 @@ Breaking changes:
- `HTML` files now open in the editor by default

## v0.3.11

- Added search and replace widget
- Added the ability to delete files on OSX with cmd+backspace
- Added the ability to set more finely grained logger levels
Expand Down
24 changes: 24 additions & 0 deletions packages/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ To get this working, you need to enable clangd's global index using the
"cpp.clangdArgs": "--background-index"
}

## Using the clang-tidy linter

Note: This functionality is available when using clangd 9 and later.

You can set the preference 'cpp.clangTidy' to enable the clang-tidy linter included in clangd. When the preference is set, there are two ways to chose which of its built-in checks clang-tidy will use:

- using the preferences: 'cpp.clangTidyChecks'
- using the file '.clang-tidy' . The file is located in the same folder of the files or a parent folder.

Note: using the preference checks will supersede the value found in the .clang-tidy file.

The syntax used to fill the checks can be found at http://clang.llvm.org/extra/clang-tidy/

clang-tidy has its own checks and can also run Clang static analyzer checks. Each check has a name ([see link above for full list](http://clang.llvm.org/extra/clang-tidy/)). Clang-tidy takes as input the checks that should run, in the form of a comma-separated list of positive and negative (prefixed with -) globs. Positive globs add subsets of checks, negative globs remove them.

There are two ways to configure clang-tidy's checks: through a Theia preference or using a .clang-tidy config file. Here are examples for both:"

- for the preferences: "cpp.clangTidyChecks": "*,-readability-*"
- Meaning: enables all list-checks and disable all readability-* checks

- for the .clang-tidy file: Checks: -*,readability-*
- Meaning: disable all list-checks and enable all readability-* checks

## License

- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
4 changes: 3 additions & 1 deletion packages/cpp/src/browser/cpp-language-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export class CppLanguageClientContribution extends BaseLanguageClientContributio
protected getStartParameters(): CppStartParameters {
return {
clangdExecutable: this.cppPreferences['cpp.clangdExecutable'],
clangdArgs: this.cppPreferences['cpp.clangdArgs']
clangdArgs: this.cppPreferences['cpp.clangdArgs'],
clangTidy: this.cppPreferences['cpp.clangTidy'],
clangTidyChecks: this.cppPreferences['cpp.clangTidyChecks']
};
}
}
12 changes: 12 additions & 0 deletions packages/cpp/src/browser/cpp-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const cppPreferencesSchema: PreferenceSchema = {
description: 'Specify the arguments to pass to clangd when starting the language server.',
default: '',
type: 'string'
},
'cpp.clangTidy': {
description: 'Enable/disable C/C++ linting.',
default: false,
type: 'boolean'
},
'cpp.clangTidyChecks': {
description: 'Specify comma separated arguments to pass to clang-tidy. Activated only if cpp.clang-tidy is enabled',
default: '',
type: 'string'
}
}
};
Expand All @@ -70,6 +80,8 @@ export class CppConfiguration {
'cpp.experimentalCommands': boolean;
'cpp.clangdExecutable': string;
'cpp.clangdArgs': string;
'cpp.clangTidy': boolean;
'cpp.clangTidyChecks': string;
}

export const CppPreferences = Symbol('CppPreferences');
Expand Down
2 changes: 2 additions & 0 deletions packages/cpp/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export const CLANGD_EXECUTABLE_DEFAULT = 'clangd';
export interface CppStartParameters {
clangdExecutable: string;
clangdArgs: string;
clangTidy?: boolean;
clangTidyChecks?: string;
}
29 changes: 28 additions & 1 deletion packages/cpp/src/node/cpp-contribution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2017 Ericsson and others.
* Copyright (C) 2017-2019 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -42,7 +42,34 @@ export class CppContribution extends BaseLanguageServerContribution {
|| undefined
);

const clangTidy = parameters && parameters.clangTidy;
const clangTidyChecks = parameters && parameters.clangTidyChecks;

if (clangTidy) {
const supportsClangTidy = await this.testSupportsClangTidy(command);
if (supportsClangTidy) {
args.push('-clang-tidy');
if (typeof clangTidyChecks === 'string' && clangTidyChecks.length > 0) {
args.push(`-clang-tidy-checks=${clangTidyChecks}`);
}
}
}
const serverConnection = await this.createProcessStreamConnectionAsync(command, args);
this.forward(clientConnection, serverConnection);
}

protected async testSupportsClangTidy(command: string): Promise<boolean> {
// clangd should fail if -clang-tidy flag is not supported
// but if it is supported, it will run forever until killed/stopped.
// to avoid that, we pass -version flag which makes it exit early.
const process = await super.spawnProcessAsync(command, ['-clang-tidy', '-version']);
return new Promise<boolean>(resolve => {
process.errorOutput.on('data', (data: string) => {
if (data.includes('-clang-tidy')) {
resolve(false);
}
});
process.errorOutput.once('close', () => resolve(true));
});
}
}

0 comments on commit e94260d

Please sign in to comment.