forked from mug896/macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.html
906 lines (549 loc) · 43.2 KB
/
diagnostics.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-6"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-6');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Diagnostics | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="./styles/website.css">
<link rel="next" href="./line_control.html" />
<link rel="prev" href="./conditionals.html" />
</head>
<body>
<div class="book"
data-level="2.3"
data-chapter-title="Diagnostics"
data-filepath="diagnostics.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="cpp.html">
<a href="./cpp.html">
<i class="fa fa-check"></i>
<b>2.</b>
CPP
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="macros.html">
<a href="./macros.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Macros
</a>
<ul class="articles">
<li class="chapter " data-level="2.1.1" data-path="function-like_macros.html">
<a href="./function-like_macros.html">
<i class="fa fa-check"></i>
<b>2.1.1.</b>
Function-like Macros
</a>
</li>
<li class="chapter " data-level="2.1.2" data-path="one_two_hash.html">
<a href="./one_two_hash.html">
<i class="fa fa-check"></i>
<b>2.1.2.</b>
# , ##
</a>
</li>
<li class="chapter " data-level="2.1.3" data-path="variadic_macros.html">
<a href="./variadic_macros.html">
<i class="fa fa-check"></i>
<b>2.1.3.</b>
Variadic Macros
</a>
</li>
<li class="chapter " data-level="2.1.4" data-path="argument_prescan.html">
<a href="./argument_prescan.html">
<i class="fa fa-check"></i>
<b>2.1.4.</b>
Argument Prescan
</a>
</li>
<li class="chapter " data-level="2.1.5" data-path="predefined_macros.html">
<a href="./predefined_macros.html">
<i class="fa fa-check"></i>
<b>2.1.5.</b>
Predefined Macros
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2.2" data-path="conditionals.html">
<a href="./conditionals.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Conditionals
</a>
</li>
<li class="chapter active" data-level="2.3" data-path="diagnostics.html">
<a href="./diagnostics.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Diagnostics
</a>
</li>
<li class="chapter " data-level="2.4" data-path="line_control.html">
<a href="./line_control.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Line Control
</a>
</li>
<li class="chapter " data-level="2.5" data-path="pragmas.html">
<a href="./pragmas.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Pragmas
</a>
</li>
<li class="chapter " data-level="2.6" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
Command-line options
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="m4.html">
<a href="./m4.html">
<i class="fa fa-check"></i>
<b>3.</b>
M4
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="m4_quotes.html">
<a href="./m4_quotes.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
Quotes
</a>
</li>
<li class="chapter " data-level="3.2" data-path="m4_positional_parameters.html">
<a href="./m4_positional_parameters.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
Positional Parameters
</a>
</li>
<li class="chapter " data-level="3.3" data-path="m4_local_variables.html">
<a href="./m4_local_variables.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
Local Variables
</a>
</li>
<li class="chapter " data-level="3.4" data-path="m4_conditionals_and_loops.html">
<a href="./m4_conditionals_and_loops.html">
<i class="fa fa-check"></i>
<b>3.4.</b>
Conditionals and Loops
</a>
</li>
<li class="chapter " data-level="3.5" data-path="m4_diverting.html">
<a href="./m4_diverting.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
Diverting
</a>
</li>
<li class="chapter " data-level="3.6" data-path="m4_input_control.html">
<a href="./m4_input_control.html">
<i class="fa fa-check"></i>
<b>3.6.</b>
Input Control
</a>
</li>
<li class="chapter " data-level="3.7" data-path="m4_file_inclusion.html">
<a href="./m4_file_inclusion.html">
<i class="fa fa-check"></i>
<b>3.7.</b>
File Inclusion
</a>
</li>
<li class="chapter " data-level="3.8" data-path="m4_builtin_macros.html">
<a href="./m4_builtin_macros.html">
<i class="fa fa-check"></i>
<b>3.8.</b>
Built-in Macros
</a>
<ul class="articles">
<li class="chapter " data-level="3.8.1" data-path="m4_arithmetic.html">
<a href="./m4_arithmetic.html">
<i class="fa fa-check"></i>
<b>3.8.1.</b>
Arithmatic
</a>
</li>
<li class="chapter " data-level="3.8.2" data-path="m4_text_handling.html">
<a href="./m4_text_handling.html">
<i class="fa fa-check"></i>
<b>3.8.2.</b>
Text Handling
</a>
</li>
<li class="chapter " data-level="3.8.3" data-path="m4_shell_commands.html">
<a href="./m4_shell_commands.html">
<i class="fa fa-check"></i>
<b>3.8.3.</b>
Shell Commands
</a>
</li>
<li class="chapter " data-level="3.8.4" data-path="m4_miscellaneous.html">
<a href="./m4_miscellaneous.html">
<i class="fa fa-check"></i>
<b>3.8.4.</b>
Miscellaneous
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.9" data-path="m4_debugging.html">
<a href="./m4_debugging.html">
<i class="fa fa-check"></i>
<b>3.9.</b>
Debugging
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="diagnostics">Diagnostics</h1>
<p><code>#error</code> 와 <code>#warning</code> 지시자를 이용한 메시지는 <code>STDERR</code> 로 출력됩니다.</p>
<h3 id="error">#error</h3>
<p>전처리 시에 fatal error 메시지를 출력할 때 사용하는 지시자 입니다.
지시자 이후에 오는 token 들은 메시지를 출력하는데 사용됩니다.
메시지에서 매크로 확장은 발생하지 않고 이어지는 spaces 들은 하나로 출력됩니다.
<code>#error</code> 지시자 위치에서 전처리가 종료되는 것은 아니고 끝까지 실행됩니다.</p>
<blockquote>
<p>전처리기 종료 상태 값으로 <code>1</code> 이 반환되고 이어지는 컴파일 작업은 중단됩니다.</p>
</blockquote>
<pre><code>/usr/include/x86_64-linux-gnu/bits/stdio.h 참조
#ifndef _STDIO_H
# error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
#endif
-------------------------------------------
/usr/include/x86_64-linux-gnu/sys/elf.h 참조
#ifdef __x86_64__
# error This header is unsupported on x86-64.
#else
# warning "This header is obsolete; use <sys/procfs.h> instead."
</code></pre><h3 id="warning">#warning</h3>
<p>전처리 시에 warning 메시지를 출력할 때 사용하는 지시자 입니다.</p>
<blockquote>
<p>전처리기 종료 상태 값은 <code>0</code> 이되고 컴파일 작업은 정상적으로 진행됩니다.</p>
</blockquote>
<pre><code>/usr/include/math.h 참조
#if defined log && defined __GNUC__
# warning A macro called log was already defined when <math.h> was included.
# warning This will cause compilation problems.
#endif
-------------------------------------------------
/usr/include/features.h 참조
#if (defined _BSD_SOURCE || defined _SVID_SOURCE) && !defined _DEFAULT_SOURCE
# warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
# undef _DEFAULT_SOURCE
# define _DEFAULT_SOURCE 1
#endif
</code></pre><p>기본적으로 <a href="https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html" target="_blank">시스템 헤더</a> 에서 발생하는 warning 은 출력되지 않습니다.
아래 첫 번째 예제를 보면 <code><stdio.h></code> 에는 <code>BUFSIZ</code> 매크로가 정의되어 있는데
동일한 매크로를 다른 값으로 정의하였으나 redefined warning 이 발생하지 않고 있습니다.
두 번째와 같이 #define 을 시스템 헤더 아래로 내려야 기존과 같이 warning 이 발생합니다.</p>
<blockquote>
<p>시스템 헤더에서 직접 #warning 지시자에 의한 메시지는 출력됩니다</p>
</blockquote>
<pre><code>$ gcpp - <<\@ > /dev/null
#define BUFSIZ 1234 # 또는 #include "myheader.h"
#include <stdio.h> # stdio.h 에 동일한 BUFSIZ 매크로가 정의되어
# 있지만 redefined warning 이 발생하지 않는다.
BUFSIZ
@
# empty
...........................................................................
$ gcpp - <<\@ > /dev/null
#include <stdio.h> # #define 을 시스템 헤더 아래로 내려야
#define BUFSIZ 1234 # 기존과 같이 warning 이 발생합니다.
BUFSIZ
@
<stdin>:2: warning: "BUFSIZ" redefined
In file included from <stdin>:1:
/usr/include/stdio.h:99: note: this is the location of the previous definition
99 | #define BUFSIZ 8192
|
</code></pre><h1 id="quiz">Quiz</h1>
<p><code>#include <signal.h></code> 할 경우 <code>sigset_t</code> 타입을 사용할 수 있는지 알려면 어떻게 할까요?</p>
<p>cpp 명령의 <code>-dD</code> 옵션을 이용하면 특정 헤더 파일이 #include 됐을 때
정의되는 매크로, 함수, typedef 정보들을 모두 볼 수가 있습니다.
하나의 헤더 파일이 #include 되면 실제로는 연관된 여러 개의 헤더 파일이 #include 되는데
이때 어떤 헤더 파일에 정의가 존재하는지도 알 수 있습니다.</p>
<p>다음을 보면 <code>#include <signal.h></code> 할 경우 <code>sigset_t</code> 타입이 정의되어 사용할 수 있고,
실제 정의가 존재하는 파일은 <code>/bits/types/sigset_t.h</code> 인 것을 알 수 있습니다. </p>
<pre><code class="lang-bash">bash$ cpp <span class="hljs-operator">-d</span>D <<< <span class="hljs-string">'#include <signal.h>'</span> | grep -w -B5 <span class="hljs-string">'sigset_t;'</span>
unsigned long int __val[(<span class="hljs-number">1024</span> / (<span class="hljs-number">8</span> * sizeof (unsigned long int)))];
} __sigset_t;
<span class="hljs-comment"># 5 "/usr/include/x86_64-linux-gnu/bits/types/sigset_t.h" 2 3 4 <---- 실제 정의가</span>
존재하는 파일
typedef __sigset_t sigset_t; <---- sigset_t 타입이 존재한다.
bash$ cpp <span class="hljs-operator">-d</span>D <<< <span class="hljs-string">'#include <signal.h>'</span> | grep -w -B5 <span class="hljs-string">'__sigset_t;'</span>
<span class="hljs-comment">#define _SIGSET_NWORDS (1024 / (8 * sizeof (unsigned long int)))</span>
typedef struct
{
unsigned long int __val[(<span class="hljs-number">1024</span> / (<span class="hljs-number">8</span> * sizeof (unsigned long int)))];
} __sigset_t;
</code></pre>
<p><code>#include <signal.h></code> 할 경우 <code>sigmask</code> 매크로를 사용할 수 있고
실제 정의는 <code>signal.h</code> 파일의 167 번 라인에 존재하는 것을 알 수 있습니다.</p>
<pre><code class="lang-bash">bash$ cpp <span class="hljs-operator">-d</span>D <<< <span class="hljs-string">'#include <signal.h>'</span> | grep -w -B3 <span class="hljs-string">'#define sigmask'</span>
extern void psiginfo (const siginfo_t *__pinfo, const char *__s);
<span class="hljs-comment"># 167 "/usr/include/signal.h" 3 4 <---- signal.h 파일의 167 번 라인</span>
<span class="hljs-comment">#define sigmask(sig) ((int)(1u << ((sig) - 1))) <---- sigmask 매크로가 존재 한다.</span>
</code></pre>
<p>매크로의 경우 확인할 수 있는 또 다른 방법은 다음과 같이 재정의 했을 때
warning 이 발생하면 정의되어 있는 것입니다.
이때도 매크로가 정의되어 있는 헤더 파일과 위치가 함께 표시됩니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># signal.h 헤더에 sigmask 매크로가 정의되어 있는지 확인</span>
bash$ cpp <<< $<span class="hljs-string">'#include <signal.h>\n#define sigmask hello'</span> > /dev/null
<stdin>:<span class="hljs-number">2</span>: warning: <span class="hljs-string">"sigmask"</span> redefined
In file included from <stdin>:<span class="hljs-number">1</span>:
/usr/include/signal.h:<span class="hljs-number">167</span>: note: this is the location of the previous definition
<span class="hljs-number">167</span> | <span class="hljs-comment"># define sigmask(sig) ((int)(1u << ((sig) - 1))) <---- 정의되어 있다.</span>
|
<span class="hljs-comment"># 다음과 같이 정의되어 있다고만 나오면 predefined 매크로입니다.</span>
bash$ cpp <<< $<span class="hljs-string">'#include <signal.h>\n#define __INTPTR_TYPE__ hello'</span> > /dev/null
<stdin>:<span class="hljs-number">2</span>: warning: <span class="hljs-string">"__INTPTR_TYPE__"</span> redefined
<built-in>: note: this is the location of the previous definition
bash$ cpp <span class="hljs-operator">-d</span>M /dev/null | grep <span class="hljs-string">'__INTPTR_TYPE__'</span>
<span class="hljs-comment">#define __INTPTR_TYPE__ long int</span>
</code></pre>
<p><code>#include <stdio.h></code> 했을 때 참조되는 모든 헤더 파일을 보려면 <code>-M</code> 옵션을 사용하면 됩니다.</p>
<pre><code class="lang-bash">bash$ cpp -M <<< <span class="hljs-string">"#include <stdio.h>"</span>
-: /usr/include/stdc-predef.h /usr/include/stdio.h \
/usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
/usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \
/usr/include/x86_64-linux-gnu/bits/wordsize.h \
/usr/include/x86_64-linux-gnu/bits/long-double.h \
/usr/include/x86_64-linux-gnu/gnu/stubs.h \
. . .
. . .
</code></pre>
<h1 id="2-">2 .</h1>
<p>다음은 shell 함수를 활용하여 특정 헤더를 #include 했을 때 정의되어 있는
함수, 매크로, typedef, struct, enum 을 출력하는 것입니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># stdio.h, math.h 헤더를 #include 했을 때 정의되어 있는 함수를 출력 ( 함수형 매크로 포함 )</span>
$ header-function stdio.h math.h
<span class="hljs-comment"># inttypes.h, limits.h 헤더를 #include 했을 때 정의되어 있는 매크로를 출력</span>
$ header-macro inttypes.h limits.h
<span class="hljs-comment"># sys/select.h 헤더를 #include 했을 때 'FD_SET(foo, &bar)' 확장 결과를 출력</span>
$ header-macro sys/select.h <span class="hljs-operator">-s</span> <span class="hljs-string">'FD_SET(foo, &bar)'</span>
<span class="hljs-comment"># 아무런 헤더를 주지 않으면 predefined macros 가 출력됩니다.</span>
$ header-macro
<span class="hljs-comment"># signal.h 헤더를 #include 했을 때 정의되어 있는 typedef 을 출력</span>
$ header-typedef signal.h
<span class="hljs-comment"># signal.h 헤더를 #include 했을 때 sigevent_t 정의를 검색</span>
$ header-typedef signal.h <span class="hljs-operator">-s</span> sigevent_t
<span class="hljs-comment"># 기타 cpp 옵션을 추가하려면 마지막에 '--' 붙이고 추가하면 됩니다.</span>
$ header-dump stdlib.h -- -D_GNU_SOURCE
</code></pre>
<pre><code class="lang-bash"><span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">dump</span></span> ()
{
<span class="hljs-built_in">local</span> hname hsum opts args search
header_args_ <span class="hljs-variable">$@</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> <span class="hljs-string">"<span class="hljs-variable">$search</span>"</span> = <span class="hljs-string">"-s"</span>; <span class="hljs-keyword">then</span>
cpp -M <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span>
<span class="hljs-keyword">else</span>
cpp <span class="hljs-operator">-d</span>D <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> | sed <span class="hljs-string">'/^# 1 "<stdin>"/,/^# 1 "<stdin>"/d'</span>
<span class="hljs-keyword">fi</span>
}
<span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">function</span></span> ()
{
<span class="hljs-built_in">local</span> hname hsum opts args
header_args_ <span class="hljs-variable">$@</span>
cpp <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> |
sed -En <span class="hljs-operator">-e</span> <span class="hljs-string">'/^\s*(__extension__\s+)?(extern|static)\s+/{ :X'</span> \
<span class="hljs-operator">-e</span> <span class="hljs-string">'/;/{ s#\s*;#;#; bY}'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'/\{/{ s#\s*\{.*# \{ ...#; bY}'</span> \
<span class="hljs-operator">-e</span> <span class="hljs-string">' N; bX; :Y /\w+\s*\([^(]/{ s/\n+/\n/; x; /__extension__\s*$/!z; G;p }'</span> \
<span class="hljs-operator">-e</span> <span class="hljs-string">' b }; s/^/\n/; h'</span>
<span class="hljs-built_in">echo</span>
cpp <span class="hljs-operator">-d</span>D <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> |
sed -En <span class="hljs-operator">-e</span> <span class="hljs-string">'/^# 1 "<stdin>"/,/^# 1 "<stdin>"/d'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'/#\s*define\s+\w+\(/p'</span> | sort -u
}
<span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">macro</span></span> ()
{
<span class="hljs-built_in">local</span> hname hsum opts args search
header_args_ <span class="hljs-variable">$@</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> -n <span class="hljs-string">"<span class="hljs-variable">$hsum</span>"</span>; <span class="hljs-keyword">then</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> -n <span class="hljs-string">"<span class="hljs-variable">$search</span>"</span>; <span class="hljs-keyword">then</span>
cpp <span class="hljs-operator">-d</span>D <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span>$<span class="hljs-string">'\n\a\n'</span><span class="hljs-variable">$search</span> |
sed -n <span class="hljs-operator">-e</span> <span class="hljs-string">'1,/\a/d'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'/^# [0-9]\+ "<stdin>"/d'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'H'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'${x; s/\n//gp}'</span>
<span class="hljs-keyword">else</span>
cpp <span class="hljs-operator">-d</span>D <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> |
sed -En <span class="hljs-operator">-e</span> <span class="hljs-string">'/^# 1 "<stdin>"/,/^# 1 "<stdin>"/d'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'/#\s*define/p'</span> | sort -u
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">else</span>
cpp <span class="hljs-operator">-d</span>M <span class="hljs-variable">$opts</span> /dev/null | sort
<span class="hljs-keyword">fi</span>
}
<span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">enum</span></span> ()
{
<span class="hljs-built_in">local</span> hname hsum opts args
header_args_ <span class="hljs-variable">$@</span>
cpp <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> | sed -En <span class="hljs-operator">-e</span> <span class="hljs-string">'/^\s*(typedef\s+)?enum/ { /\(/{ /=/!b }'</span> \
<span class="hljs-operator">-e</span> <span class="hljs-string">':X /;\s*$/{p;b}; /^\s*$|^#/!p; n; bX }'</span>
}
<span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">typedef</span></span> () { header_typedef_struct_ <span class="hljs-variable">$@</span> ;}
<span class="hljs-keyword">function</span> header-<span class="hljs-function"><span class="hljs-title">struct</span></span> () { header_typedef_struct_ <span class="hljs-variable">$@</span> ;}
<span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">header_typedef_struct_</span></span> ()
{
<span class="hljs-built_in">local</span> hname hsum opts args search
<span class="hljs-built_in">local</span> AAA BBB
header_args_ <span class="hljs-variable">$@</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> <span class="hljs-string">"<span class="hljs-variable">${FUNCNAME[1]}</span>"</span> = <span class="hljs-string">"header-typedef"</span>; <span class="hljs-keyword">then</span>
AAA=<span class="hljs-string">'/typedef/ {
if ( /^.*{.*typedef/ ) sub( /{/, ""); sub( /\s*};$/, "");'</span>
BBB=<span class="hljs-string">'if ( res ~ /}/ ) { if ( res ~ Q"[^}]*;$" ) print res }
else { if ( res ~ Q"[^ ]*;$" ) print res }'</span>
<span class="hljs-keyword">else</span>
AAA=<span class="hljs-string">'( /(struct|union)\s+\w+/ || /typedef\s+(struct|union)/ ) && !/^# [0-9]+/ {
if ( !/{/ && /[;,(*]/ ) next;'</span>
BBB=<span class="hljs-string">'if ( res ~ "^[^{]*(struct|union)\\s+[^{]*"Q || res ~ Q"[^}]*;$" )
{ print res }'</span>
<span class="hljs-keyword">fi</span>
cpp <span class="hljs-variable">$opts</span> <<< <span class="hljs-variable">$hsum</span> |
gawk -v Q=<span class="hljs-string">"<span class="hljs-variable">$search</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$AAA</span>"</span><span class="hljs-string">'
res = $0
while ( !/{$/ && !/^\s*{/ )
if (/;$/) { printr(); next } else { getline
if (!/^\s*$/) res = res "\n" gensub(/^\s*/,"",1) }
if (/{$/) depth=1; else depth=0
while( getline > 0 ) {
if (/^\s*$/ || /^# [0-9]+/) continue
sub(/^\s*/, space( /^\s*}/ ? depth - 1 : depth ))
res = res "\n" $0
if (/^\s*{/) { depth++; continue; }
if (/^\s*}/) { depth--; if ( depth == 0 ) { printr(); break; } }
}
}
function printr() {
while ( res !~ /;$/ ) { getline; res = res" "$0 }
if ( Q != "" ) { '</span><span class="hljs-string">"<span class="hljs-variable">$BBB</span>"</span><span class="hljs-string">' } else print res
res = ""
}
function space(n, r) { for( ; n > 0; n-- ) r = r " "; return r }
'</span>
}
<span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">header_args_</span></span> ()
{
<span class="hljs-built_in">local</span> file
<span class="hljs-built_in">read</span> -t <span class="hljs-number">0</span> && { args=<span class="hljs-string">""</span>
<span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> -r line; <span class="hljs-keyword">do</span> args+=<span class="hljs-string">" <span class="hljs-variable">$line</span>"</span>; <span class="hljs-keyword">done</span>
<span class="hljs-built_in">set</span> <span class="hljs-operator">-f</span>; <span class="hljs-built_in">set</span> -- <span class="hljs-variable">$@</span> <span class="hljs-variable">$args</span>; <span class="hljs-built_in">set</span> +f
}
<span class="hljs-keyword">while</span> <span class="hljs-built_in">test</span> <span class="hljs-variable">$#</span> <span class="hljs-operator">-gt</span> <span class="hljs-number">0</span>; <span class="hljs-keyword">do</span>
<span class="hljs-built_in">test</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> = <span class="hljs-string">"--"</span> && { <span class="hljs-built_in">shift</span>; opts=<span class="hljs-string">" <span class="hljs-variable">$@</span>"</span>; <span class="hljs-built_in">break</span> ;}
<span class="hljs-built_in">test</span> <span class="hljs-string">"<span class="hljs-variable">${1:0:2}</span>"</span> = <span class="hljs-string">"-s"</span> && {
<span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> <span class="hljs-string">"<span class="hljs-variable">${FUNCNAME[1]}</span>"</span> = <span class="hljs-string">"header-dump"</span>; <span class="hljs-keyword">then</span>
search=<span class="hljs-string">"-s"</span>
<span class="hljs-keyword">else</span>
search=<span class="hljs-variable">${1:2}</span>
<span class="hljs-built_in">test</span> -z <span class="hljs-string">"<span class="hljs-variable">$search</span>"</span> && { <span class="hljs-built_in">shift</span>; search=<span class="hljs-variable">$1</span> ;}
<span class="hljs-keyword">fi</span>
<span class="hljs-built_in">shift</span>; <span class="hljs-built_in">continue</span>
}
file=$( <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> | sed -En <span class="hljs-operator">-e</span> <span class="hljs-string">'s/^.*<([[:alnum:]_/]+(\.h)?)>.*$/\1/'</span> \
<span class="hljs-operator">-e</span> <span class="hljs-string">'tX; /^#/b; /^include$/!{ :X p }'</span> )
<span class="hljs-built_in">test</span> -n <span class="hljs-string">"<span class="hljs-variable">$file</span>"</span> && hsum+=<span class="hljs-string">"#include <<span class="hljs-variable">$file</span>>"</span>$<span class="hljs-string">'\n'</span>
<span class="hljs-built_in">shift</span>
<span class="hljs-keyword">done</span>
}
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-dump
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-function
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-macro
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-enum
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-typedef
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header-struct
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header_args_
<span class="hljs-built_in">export</span> <span class="hljs-operator">-f</span> header_typedef_struct_
</code></pre>
<h1 id="3-">3 .</h1>
<p><code>ptrdiff_t</code> 나 <code>size_t</code> 같은 타입이 어느 헤더 파일에 있는지는 어떻게 알 수 있을까요?</p>
<pre><code class="lang-bash">$ grep -Irw <span class="hljs-string">'ptrdiff_t\s*;'</span> /usr/{include,lib}
. . .
$ grep -Irw <span class="hljs-string">'size_t\s*;'</span> /usr/{include,lib}
. . .
</code></pre>
</section>
</div>
</div>
</div>
<a href="./conditionals.html" class="navigation navigation-prev " aria-label="Previous page: Conditionals"><i class="fa fa-angle-left"></i></a>
<a href="./line_control.html" class="navigation navigation-next " aria-label="Next page: Line Control"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>