1// RUN: %check_clang_tidy %s readability-duplicate-include %t -- -- -isystem %S/Inputs/duplicate-include/system -I %S/Inputs/duplicate-include
2
3int a;
4#include <string.h>
5int b;
6#include <string.h>
7int c;
8// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include [readability-duplicate-include]
9// CHECK-FIXES: {{^int a;$}}
10// CHECK-FIXES-NEXT: {{^#include <string.h>$}}
11// CHECK-FIXES-NEXT: {{^int b;$}}
12// CHECK-FIXES-NEXT: {{^int c;$}}
13
14int d;
15#include <iostream>
16int e;
17#include <iostream> // extra stuff that will also be removed
18int f;
19// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include
20// CHECK-FIXES: {{^int d;$}}
21// CHECK-FIXES-NEXT: {{^#include <iostream>$}}
22// CHECK-FIXES-NEXT: {{^int e;$}}
23// CHECK-FIXES-NEXT: {{^int f;$}}
24
25int g;
26#include "duplicate-include.h"
27int h;
28#include "duplicate-include.h"
29int i;
30// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include
31// CHECK-FIXES: {{^int g;$}}
32// CHECK-FIXES-NEXT: {{^#include "duplicate-include.h"$}}
33// CHECK-FIXES-NEXT: {{^int h;$}}
34// CHECK-FIXES-NEXT: {{^int i;$}}
35
36#include <types.h>
37
38int j;
39#include <sys/types.h>
40int k;
41#include <sys/types.h>
42int l;
43// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include
44// CHECK-FIXES: {{^int j;$}}
45// CHECK-FIXES-NEXT: {{^#include <sys/types.h>$}}
46// CHECK-FIXES-NEXT: {{^int k;$}}
47// CHECK-FIXES-NEXT: {{^int l;$}}
48
49int m;
50 # include <string.h> // lots of space
51int n;
52// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: duplicate include
53// CHECK-FIXES: {{^int m;$}}
54// CHECK-FIXES-NEXT: {{^int n;$}}
55
56// defining a macro in the main file resets the included file cache
57#define ARBITRARY_MACRO
58int o;
59#include <sys/types.h>
60int p;
61// CHECK-FIXES: {{^int o;$}}
62// CHECK-FIXES-NEXT: {{^#include <sys/types.h>$}}
63// CHECK-FIXES-NEXT: {{^int p;$}}
64
65// undefining a macro resets the cache
66#undef ARBITRARY_MACRO
67int q;
68#include <sys/types.h>
69int r;
70// CHECK-FIXES: {{^int q;$}}
71// CHECK-FIXES-NEXT: {{^#include <sys/types.h>$}}
72// CHECK-FIXES-NEXT: {{^int r;$}}
73
74namespace Issue_87303 {
75#define RESET_INCLUDE_CACHE
76// Expect no warnings
77
78#define MACRO_FILENAME "duplicate-include.h"
79#include MACRO_FILENAME
80#include "duplicate-include.h"
81
82#define MACRO_FILENAME_2 <duplicate-include2.h>
83#include <duplicate-include2.h>
84#include MACRO_FILENAME_2
85
86#undef RESET_INCLUDE_CACHE
87} // Issue_87303
88

source code of clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp