Skip to content

Commit

Permalink
use clang-tidy command with task to generate items in problem's view
Browse files Browse the repository at this point in the history
clang-tidy uses the .clang-tidy file checks
When you define in "task.json" a problem matcher with "$clangTidyMatcher",
it creates an entry in the "problems" view with a tag: "clang-tidy-task"
The result in the "Problems" view is persistent until you
re-generates the task.

Signed-off-by: Jacques Bouthillier <[email protected]>
  • Loading branch information
lmcbout committed Jun 21, 2019
1 parent ef85ac7 commit 4d6ff20
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
28 changes: 25 additions & 3 deletions packages/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ To get this working, you need to enable clangd's global index using the

## Using the clang-tidy linter

Note: This functionality is available when using clangd 9 and later.
**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:
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 choose 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.
**Note**: When the preference setting for "cpp.clangTidyChecks" is set, the configs will be merged with the configuration found in ".clang-tidy" file. If you want to drop the configs from ".clang-tidy", you'd need to disable it in "cpp.clangTidyChecks" with **"cpp.clangTidyChecks": "-*"**.

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

Expand All @@ -102,6 +102,28 @@ There are two ways to configure clang-tidy's checks: through a Theia preference
- for the .clang-tidy file: Checks: "-*,readability-*"
- Meaning: disable all list-checks and enable all readability-* checks

### Using clang-tidy as a task

In .theia/tasks.json, add the following:

```json
{
"label": "[Task] clang-tidy",
"type": "shell",
"cwd": "${workspaceFolder}",
"command": "clang-tidy",
"args": [
"*"
],
"options": {},
"problemMatcher": [
"$clangTidyMatcher"
]
}
```

If you want a description for each task field, see [theia/packages/task/src/browser/task-schema-updater.ts]( https://github.com/theia-ide/theia/blob/531aa3bde8dea7f022ea41beaee3aace65ce54de/packages/task/src/browser/task-schema-updater.ts#L62 )

## License

- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
Expand Down
8 changes: 6 additions & 2 deletions packages/cpp/src/browser/cpp-task-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import { TaskResolverRegistry } from '@theia/task/lib/browser/task-contribution'
import { CppBuildConfigurationManager, CppBuildConfiguration } from './cpp-build-configurations';
import { Event } from '@theia/core';
import { expect } from 'chai';
import { TaskConfiguration } from '@theia/task/src/common';
import { TaskConfiguration } from '@theia/task/lib/common';
import { ProcessTaskConfiguration } from '@theia/task/lib/common/process/task-protocol';
import { TaskDefinitionRegistry } from '@theia/task/lib/common/task-protocol';
import { TaskDefinitionRegistry, ProblemMatcherRegistry, ProblemPatternRegistry } from '@theia/task/lib/common/task-protocol';
import { TaskDefinitionRegistryImpl } from '@theia/task/lib/browser/task-definition-registry';
import { ProblemMatcherRegistryImpl } from '@theia/task/lib/browser/task-problem-matcher-registry';
import { ProblemPatternRegistryImpl } from '@theia/task/lib/browser/task-problem-pattern-registry';

// The object under test.
let taskProvider: CppTaskProvider;
Expand Down Expand Up @@ -71,6 +73,8 @@ beforeEach(function () {
container.bind(TaskResolverRegistry).toSelf().inSingletonScope();
container.bind(TaskDefinitionRegistry).to(TaskDefinitionRegistryImpl).inSingletonScope();
container.bind(CppBuildConfigurationManager).to(MockCppBuildConfigurationManager);
container.bind(ProblemMatcherRegistry).to(ProblemMatcherRegistryImpl).inSingletonScope();
container.bind(ProblemPatternRegistry).to(ProblemPatternRegistryImpl).inSingletonScope();
taskProvider = container.get(CppTaskProvider);

// Register a task resolver of type 'shell', on which the cpp build tasks
Expand Down
24 changes: 24 additions & 0 deletions packages/cpp/src/browser/cpp-task-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import parseArgv = require('string-argv');
import { inject, injectable, postConstruct } from 'inversify';
import { ProblemMatcherRegistry, ProblemPatternRegistry } from '@theia/task/lib/common/task-protocol';
import { ProcessTaskConfiguration } from '@theia/task/lib/common/process/task-protocol';
import { TaskContribution, TaskProvider, TaskProviderRegistry, TaskResolver, TaskResolverRegistry } from '@theia/task/lib/browser/task-contribution';
import { CppBuildConfigurationManager, CppBuildConfiguration } from './cpp-build-configurations';
Expand All @@ -37,10 +38,33 @@ export class CppTaskProvider implements TaskContribution, TaskProvider, TaskReso
@inject(TaskResolverRegistry) protected readonly taskResolverRegistry: TaskResolverRegistry;
@inject(TaskDefinitionRegistry) protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(CppBuildConfigurationManager) protected readonly cppBuildConfigurationManager: CppBuildConfigurationManager;
@inject(ProblemMatcherRegistry) protected readonly problemMatcherRegistry: ProblemMatcherRegistry;
@inject(ProblemPatternRegistry) protected readonly problemPatternRegistry: ProblemPatternRegistry;

@postConstruct()
protected init(): void {
this.registerTaskDefinition();
this.problemPatternRegistry.register({
'name': 'clangTidyPattern',
'regexp': '^(.+):(\\d+):(\\d+):\\s+(error|warning|info|note):\\s+(.+?)\\s+\\[(.+)\\]$',
'file': 1,
'line': 2,
'character': 3,
'severity': 4,
'message': 5,
'code': 6
});
this.problemMatcherRegistry.register({
'name': 'clangTidyMatcher',
'label': 'Clang-tidy problems',
'owner': 'clang-tidy',
'source': 'clang-tidy-task',
'applyTo': 'alldocuments',
'fileLocation': [
'absolute'
],
'pattern': 'clangTidyPattern'
});
}

registerProviders(registry: TaskProviderRegistry) {
Expand Down

0 comments on commit 4d6ff20

Please sign in to comment.